Custom bar button item not performing segue - ios

I have a right bar button item as defined in my viewDidLoad:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
UIButton *helpButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:helpButton ];
}
I had linked the bar button item and performed segue with identifier but it's not pushing the view. Why is that so?
- (IBAction)btnShowHelp:(id)sender {
[self performSegueWithIdentifier:#"showHelp" sender:self];
}

delete the link between your button and - (IBAction)btnShowHelp:(id)sender, change your method to - (void)btnShowHelp(id)sender, then change your code in viewDidLoad like this :
UIButton *helpButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
[helpButton addTarget:self action:#selector(btnShowHelp:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:helpButton ];

Turns out that btnShowHelp that was in my storyboard was already replaced by the new UIButton defined in viewDidLoad and there was no need for it.
https://developer.apple.com/reference/uikit/uibarbuttonitem/1617151-initwithcustomview?language=objc
The bar button item created by this method does not call the action method of its target in response to user interactions. Instead, the bar button item expects the specified custom view to handle any user interactions and provide an appropriate response.
I was doing [self.navigationItem.rightBarButtonItem setAction:] which I then realised I was suppose to set the action for my UIButton instead of the bar button item...
UIButton *helpButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
[helpButton addTarget:self action:#selector(showHelp) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:helpButton ];
- (void)showHelp {
[self performSegueWithIdentifier:#"showHelp" sender:self];
}
Adding action and target to a custom Navigation BarButtonItem?

Related

Use another button to do the same thing as the back button

What I have now?
I have two view controllers (VCA & VCB) and a navigation controller. VCA can push to VCB and VCB can go back to VCA by pressing back button in the navigation bar of VCB.
What I want to do?
I want to add another button in VCB to do the same thing as the back button in navigation bar does, that is when that button is pressed, I should be lead back to VCA, how can I do it?
What I have tried?
I tried to add a button in VCB and controller drag a line from the button to VCA so that they are connected, but I found that VCA's viewDidLoad method is called each time I back from VCB, this is not what I wanted.
Thanks!
You can realize some button (object of UIButton class), for example like this:
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
backButton.frame = CGRectMake(60.0f, 40.0f, 200.0f, 50.0f);
backButton.titleLabel.font = [UIFont boldSystemFontOfSize:28.0f];
backButton.titleLabel.shadowOffset = CGSizeMake (0.0f, 1.0f);
[backButton setTitle:#"Back" forState:UIControlStateNormal];
[backButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[backButton setTitleShadowColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(goBack) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:backButton];
And also you must to define corresponding selector for action of the button:
- (void)goBack {
[self.navigationController popViewControllerAnimated:YES];
}
Now, if you click this button you will come to previous view controller.
The action that is fired when you press a back button to pop a ViewController is:
popViewControllerAnimated:(BOOL)animated
So what you can do it to create a button and add that action to it:
UIButton *goBackButton = [[UIButton alloc] initWithFrame:CGRectMake(0,64,50,50)];
[goBackButton addTarget:self action:#selector(goBackAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:goBackButton];
Now you actually have to implement that goBackAction:
-(IBAction)goBackAction:(id)sender{
[self.navigationController popViewControllerAnimated:YES];
}
You could actually add the popViewControllerAnimated action to the button, like:
[goBackButton addTarget:self.navigationController action:#selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];
The problem is that you are not able to pass that animated parameter to the method which will result in it using some junk that happens to be where that parameter was expected to be in memory. The result will be the pop sometimes being animated and other times not.

Adding button programmatically to perform segue

I am trying to create a button 'on the fly' using code, and i want that button to perform
segue to a different controller. As far as i know, creating segue programmatically is not possible, but how can i get pass that if the created button is not on my storyboard?
This is the code for creating the button:
- (void)addButton:(UIView*)view inLocation:(CGPoint)point
{
UIButton *but=[UIButton buttonWithType:UIButtonTypeRoundedRect];
but.frame= CGRectMake(point.x, point.y,30,30);
[but setTitle:#"CHOOSE" forState:UIControlStateNormal];
// The nilSymbol should call a method that will perform the segue
[but addTarget:self action:#selector(nilSymbol) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:but];
}
Any options to do that?
In Interface Builder, create a segue.
Since you can't connect the segue to the button (it doesn't exist yet), simply connect the segue to the starting ViewController.
Then, when you create the button:
// ....
[but addTarget:self action:#selector(someMethod) forControlEvents:UIControlEventTouchUpInside];
// ....
And in the someMethod:
-(void)someMethod
{
[self performSegueWithIdentifier:#"segueIdentifier" sender:self];
}

Ios How to a add a button to my TabBarController then trigger the presentation of a modal view from it

I have a UITabBarController bassed app. I'm instantiating it from the app delegate and adding a custom button in the tab bar controller. when that button is clicked, I want to present another view modally, but I cant seem to figure out how to do it. to add the button I'm basically doing this
UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
[self.tabBarController.view addSubview:button];
[button addTarget:self action:#selector(showModalViewController:) forControlEvents:UIControlEventTouchUpInside];
and also in the app delegate I have a method
- (void) showModalViewController {
DummyViewController *addController = [[DummyViewController alloc]
initWithNibName:#"DummyViewController" bundle:nil];
//addController.delegate = self;
self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:0];
// Create the navigation controller and present it modally.
[self.tabBarController.selectedViewController presentModalViewController:addController animated:YES];
// The navigation controller is now owned by the current view controller
}
I keep getting unrecognized seletor
Your selector looks for a method named showModalViewController: but your actual method is named showModalViewController. Change one or the other.
Either change the selector to #selector(showModalViewController) to match the existing method, or change the method to:
- (void)showModalViewController:(UIButton *)button {
Don't change both.

Set Action Listener Programmatically in IOS

Hi I have created a button programmatically. I will add this button to the navigation bar. Now I want to add a Touch Up Inside action listener to it. How do I do it? Thanks.
A UIButton is a subclass of UIControl.
All you need to do after creating an button is to set the target and action of the button. i.e.
// Create your button:
UIButton *button = // However you create your button
// Set the target, action and event for the button
[button addTarget:// the object that implements the action method, or nil if you want it to propagate up the responder chain.
action:// A selector for the method
forControlEvents:UIControlEventTouchUpInside];
Since you've added them to a navigation bar, it's slightly different, but basically the same. You will add the listener/handler at the same time you create the button(s). Here I've added << and >> to a navigation bar using the following:
UIBarButtonItem *nextButton = [[UIBarButtonItem alloc] initWithTitle:#">>" style:UIBarButtonItemStylePlain target:self action:#selector(navNextButtonPressed)];
UIBarButtonItem *prevButton = [[UIBarButtonItem alloc] initWithTitle:#"<<" style:UIBarButtonItemStylePlain target:self action:#selector(navPrevButtonPressed)];
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:nextButton, prevButton, nil];
and then create your handlers as normal:
#pragma mark - button handling
-(void)navNextButtonPressed
{
NSLog(#"Next pressed");
}
-(void)navPrevButtonPressed
{
NSLog(#"Prev pressed");
}

how to raise modal view from tab bar button

I'm using this:
https://github.com/boctor/idev-recipes/tree/master/RaisedCenterTabBar
Anyone know how to get a modal view raised from say..the camera button (if the camera button were to be used for something else other than the camera)?
In BaseViewController.m the centre button is added in
-(void) addCenterButtonWithImage:(UIImage*)buttonImage highlightImage:(UIImage*)highlightImage
Simply add the observer:
[button addTarget:self action:#selector(click:) forControlEvents:UIControlEventTouchUpInside];
And then obviously implement:
-(void)click:(id)sender{
[self presentModalViewController:[[UIViewController alloc] init] animated:YES];
}

Resources