Make UIButton act as navigationcontroller - ios

How can I make a regular UIButton act as a navigationcontroller so that when it's pressed I can open up a new view?

Create yourButton in viewDidLoad method as following way...
UIButton *yourButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[yourButton setTitle:#"YearButton" forState:UIControlStateNormal];
yourButton.frame = CGRectMake(240, 40, 75, 30);
[yourButton addTarget:self action:#selector(buttonSelected:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:yourButton];
This is your method code and createViewController which is object of CreateViewController class and it is declared in .h file.....
-(void) buttonSelected:(id)sender{
if (!createViewController) {
createViewController = [[CreateViewController alloc] initWithNibName:#"CreateViewController" bundle:nil];
}
[self.navigationController pushViewController:createViewController animated:YES];
}
I think it will be helpful to you.

Assuming you are using a UINavigation controller in your project.
then for the buttons action just call
[yourUIButton addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
and for the method that gets called just do the following:
-(void)buttonAction{
[self.navigationController pushViewController:YourViewController animated:YES];
}

Related

How I can get reference to UIViewController from any of it's subview(UIButton, UITextField etc)?

I want the reference of UIViewController when I have the reference to a button that is added to this UIViewController.
the situation is like a added a button to UIViewController from a different class method and now to preform action on that button I want reference to UIViewController
button is added in following way
UIViewController *controller = (id)viewController;
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:image forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, 17, 17);
[button addTarget:self action:#selector(back:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
controller.navigationItem.leftBarButtonItem = customBarItem;
here is the code for button action
- (void)back:(id)button
{
UIButton *btn = (id)button;
//[btn.UIViewController.navigationController popViewControllerAnimated:YES];
}
I tried this but it's not working
UIWindow *window = [UIApplication sharedApplication].keyWindow;
UIViewController *vc = window.rootViewController;
[vc.navigationController popViewControllerAnimated:YES];

iOS Button action doesn't work

I have a strange issue with iOS UIButton.
I have a UIViewController class:
#interface CustomViewController : UIViewController
Inside of this class I have a label called customLabel. In this label I have a button subview:
UIButton *addButton = [[UIButton alloc] init];
[customButton setImage:[UIImage imageNamed:#"image"] forState:UIControlStateNormal];
[customButton addTarget:self action:#selector(customButtonClicked) forControlEvents:UIControlEventTouchUpInside];
[customLabel insertSubview:customButton atIndex:1];
And of course, action method:
- (void)customButtonClicked
{
NSLog(#"- (void)customButtonClicked");
}
I also have a subclass for this view controller:
#interface MyNewCustomViewController : CustomViewController
This view controller is loaded inside UIWindow:
[self.navigationController pushViewController: myNewCustomViewController animated:YES];
Everything seems to be fine, label with button are at the correct positions, except the fact, that button click doesn't call action method...
Maybe you know, where the problem could be?
As long as you don't have another view over top of your current button and user interaction is enabled on the view. This should work.
// First make sure userInteractionEnabled is enabled for your custom label which will contain your button as a subview
customLabel.userInteractionEnabled = YES;
// Then create and add your button as a subview
UIButton *addButton = [[UIButton alloc] init];
[customButton setImage:[UIImage imageNamed:#"image"] forState:UIControlStateNormal];
[customButton addTarget:self action:#selector(customButtonClicked) forControlEvents:UIControlEventTouchUpInside];
[customLabel addSubview:addButton]; // Places view on top index

How to make buttons that go to a different screen?

Hey I'm having a lot of trouble with this idea I have. How do I make buttons that go to a different screen but on the CODE?
Basically depending on a value that may or may not be different for every user (Let's say the value is in x already) it will make a list of buttons...
When you click the list of buttons, they each individually go to a different screen.
How do I do this? I know how to on storyboard.. but code wise?
Edit: I SHOULD note that for the buttons specifically I have this and it is working in the code (didn't use storyboard):
UIButton * btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(50, 200, 200, 50);
[btn setTitle:#"Button 1" forState:UIControlStateNormal];
[self.view addSubview:btn];
First add a target to your button:
//Add target to your button, to call a method that presents the next ViewController:
UIButton *yourButton = [[UIButton alloc]init];
[yourButton addTarget:self action:#selector(goToNextPageMethod) forControlEvents:UIControlEventTouchUpInside];
User one of the two, case you are using a UINavigation controller or not, place them in the method you are calling when the button is pressed (the target)
//Normal presentation
YourViewController *vc = [[YourViewController alloc]init];
[self presentViewController:vc animated:YES completion:nil];
//Navgiation bar
YourViewController *vc = [[YourViewController alloc]init];
[self.navigationController pushViewController:vc animated:YES];
Hope this helps
You can attach an action to your button by doing the following:
UIButton * btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(50, 200, 200, 50);
[btn setTitle:#"Button 1" forState:UIControlStateNormal];
[btn addTarget:self action:#selector(someAction) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
and then in your someAction method, you push the view modally or via your navigation controller
- (void)someAction
{
//init your view controller here....
YourViewController *vc = [[YourViewController alloc]init];
[self.navigationController pushViewController:vc animated:YES];
}

ACTION for UIBUTTON in iOS

I have one custom class with subclass of UIView, inside this I created one button dynamically.
Now I want to set Action for button method. I set normal like UIviewController.
But its not working.
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
UIButton *but=[UIButton buttonWithType:UIButtonTypeDetailDisclosure];
but.frame= CGRectMake(200, 15, 15, 15);
[but setTitle:#"Ok" forState:UIControlStateNormal];
[but addTarget:self action:#selector(aMethod)
forControlEvents:UIControlEventTouchUpInside];
[self addSubview:but];
// Initialization code
}
return self;
}
-(void)aMethod
{
NextEyeViewController *nexteye=[[NextEyeViewController alloc]initWithNibName:#"NextEyeViewController" bundle:nil];
[self presentViewController:nexteye animated:YES completion:nil];
}
-(void)aMethod is my method name for button
If you want to set an action to your button created programmatically you need to do like this :
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
[button addTarget:self
action:#selector(aMethod)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(0.0, 0.0, 160.0, 40.0);
[view addSubview:button];
Create UIButton:
UIButton *yourButton = [[UIButton alloc] initWithFrame:frameButton];
// Here code to set button properties: color, label...
[yourButton addTarget:self action:#selector(aMethod) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:yourButton];
Your method has to receive the sender of the action:
-(void)aMethod:(id)sender {
// your code
}
Update:
You also have to call your method suitably with #selector(aMethod:) instead of #selector(aMethod).

dismissViewControllerAnimated not working

I am using Storyboard with IOS6. I am trying to go back to the previous viewcontroller but it is not working.
I customized my backbutton here.
UIButton *btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
btnBack.frame = CGRectMake(0, 0, 45,35);
[btnBack setTitle:#"Back" forState:UIControlStateNormal];
[btnBack.titleLabel setFont: [UIFont fontWithName:#"Courier" size:15]];
btnBack.layer.cornerRadius=5.0;
btnBack.layer.borderWidth=2.0;
[btnBack setBackgroundColor:[UIColor colorFbBlue]];
btnBack.layer.borderColor=[UIColor colorFbBlue].CGColor;
[btnBack setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[btnBack addTarget:self action:#selector(Click_On_Btn_Back) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithCustomView:btnBack];
-(void)Click_On_Btn_Back{
[self dismissViewControllerAnimated:YES completion:nil];
}
This is how i push segue from previous view controller.
if([segue.identifier isEqualToString:#"segueFbShare"]){
FbShareViewController *fbVC=[segue destinationViewController];
fbVC.imageUrl=self.product.ImageUrl;
}
Going to previous UIViewController when using a UINavigationController:
[self.navigationController popViewControllerAnimated:YES];
Put that line of code in your Click_On_Btn_Back method
When using a Navigation Controller and push you need to remove the view by using:
- (void)Click_On_Btn_Back {
[self.navigationController popViewControllerAnimated:YES];
}
Also Click_On_Btn_Back is not general iOS naming convention. You should use something more like: clickOnBtnBack (CamelCase).

Resources