How to assign actions to UINavigationItem backBarButtonItem - ios

I am trying to find a way to stop some of the processes within a detail controller (and let the user know that this is happening) when the back button in the navigation bar is pressed. However, I can't find a way to implement these changes when the button is pressed.
Is there a way to do this?

If you want to raise a user alert/notification before going back, you are really going to have to create your own back bar button item and assign it to self.navigationItem.leftBarButtonItem.
You need to hide the default button using:
self.navigationItem.hidesBackButton = YES;
Then add a target to your new button which does your process cleanup and raises an alert. In the alert handler, pop the controller once the user has acknowledged the alert.

The fastest and easiest way is to use custom back button like below;
-(void)viewDidLoad{
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStyleBordered target:self action:#selector(actionBack)];
[self.navigationItem setBackBarButtonItem:barButtonItem];
}
-(void)actionBack{
//PopViewController
}

Like any other button.
Put this in the .m file.
- (IBAction)saveButton:(id)sender {
//actions
}
Then control drag from the UINavigationBarButton to the IBAction

Related

How to add a button to a UINavigation item?

I am converting an app that is written in ios 2 to ios 9. I am not really familiar with ios 2 and I need some help. Back in ios 2 there were no view controllers and xib files were used. There was one xib file called the main window which would hold all the navigation items. In my case the main window xib has a navigation bar. And in code there is the following line
self.navigationItem.title = #"Back";
So I could see when i run the app on a simulator, in one of my views there is a button with titled "Back". But i cannot find a method for this button. Like there is no IBAction for it or a method. I am trying to add a selector for the navigationitem but it doesn't work. This is what I tried:
self.navigationItem.selector = #selector(backButtonClicked:)
How can I add a selector to a navigationItem so I can search that in the project or create my own selector.
You can add custom back button to your navigation bar this way
UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStyleBordered target:self action:#selector(backButtonClicked:)];
self.navigationItem.leftBarButtonItem = btn;
-(void)backButtonClicked: (id)sender
{
[self.navigationController popViewControllerAnimated: YES]; // or popToRoot... if required.
}

UINavigationController – Override Back button action

I have a view controller in a navigation stack that represents a form that the user can fill out. The form has a handful of text fields and an "Apply" button at the bottom. If the user taps the native back button on the navigation bar after entering some information, I want the user to be prompted with a confirmation message: "Your changes have not been saved. Are you sure you want to go back?" But I'm not sure of a way I can "legally" override the action of the native back button. Is this possible?
In Objective C you can override the navigationShouldPopOnBackButton function of the ViewController class to display a prompt message to the user
-(BOOL) navigationShouldPopOnBackButton {
// Do your logic
return NO;
}
You need to set UINavigationItem to the custom button in you UIViewController class.
After that you can execute your custom code in the backButtonTouch: function:
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(#"Back", nil)
style:UIBarButtonItemStyleDone
target:self
action:#selector(backButtonTouch:)];

How to implement button Done in Navigation Controller between many tab bar?

Main screen in my app has 2 buttons. When click button 1, show ViewController1. When click button 2, show ViewController2. ViewController1 has 2 bar items. ViewController of each bar item has Back and Done button. Back is back to main menu, Done is used to hide keyboard. I want to control 2 these buttons.
I have 2 directions:
Add Navigation Controller at main screen. It has Back button. Button Done is implemented in ViewController of each bar item. In this case, button Done works not good when change tab bar. I loged and see that, first click in tab bar, it works correct, but click Item1->Item2->Item1, button Done in Item1 this time not correct, because it is still button Done in Item2 Controller.
How to fix in this case?
I hide Navigation Controller in main screen, implement Navigation Controller in Controller of each Tab Bar. In this case, button Done works good, but button Back can't move to main screen when click on it.
How to move to main screen in this case?
Code in AppDelegate.m:
UIViewController *cont = [[VCMainMenu alloc]initWithNibName:#"VCMainMenu" bundle:nil];
self.navController = [[UINavigationController alloc]initWithRootViewController:cont];
[self.window setRootViewController:navController];
Code in MainMenu.m, ViewDidLoad:
self.navigationItem.backBarButtonItem =
[[UIBarButtonItem alloc] initWithTitle:#"Back"
style:UIBarButtonItemStylePlain
target:nil
action:nil];
Code in MainMenu.m, buttonClick:
self.navigationController.navigationBarHidden = YES;
[self.tab setSelectedIndex:0];
[self.navigationController pushViewController:self.tab animated:YES];
Button Done in each class:
UIBarButtonItem *btnDone = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(btnDonePressed:)];
self.navigationController.topViewController.navigationItem.rightBarButtonItem = btnDone;
btnDone.enabled = TRUE;
btnDone.style = UIBarButtonSystemItemDone;
Thanks.
To hide the Keyboard you need to know which textView is firstResponder. You have to implement a dismisskeyboard method for each of your scenes.
Also, why are you coding all the navigation stuff? It would be much easier implementing them via storyboards and segues.
I hide Navigation Controller in main screen, implement Navigation
Controller in Controller of each Tab Bar. In this case, button Done
works good, but button Back can't move to main screen when click on
it. How to move to main screen in this case?
To do that, use
[self.navigationController popViewControllerAnimated:YES];
And as a sidenote #Marcal has a point, I think it might be better if you use storyboards and segues

How can i assign an action to rightbarbuttonitem with an edit style?

I have an edit rightbarbuttonitem in my view in navigation bar. I set it up with the help of storyboard/IB, not programmatically. Now, all i want is to assign an action when the "done" barbuttonitem is pressed (not edit).
Is there a way to achieve it? I tried manually through -(IBAction), but it's not working. Also, i want to perform the action on selected items in UITableView. So if you give me an idea, it would be great.
That button calls the method
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
You can implement it and it will get called everytime your edit/done button gets tapped. All you have to do is check the button's title property to see when it's showing done and when it's showing edit
If you declared your button as an IBOutlet then all you'd need to do is use the synthesised variable on your .m as so:
_yourBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(runMethod)];
self.navigationItem.rightBarButtonItem = _yourBarButton;
Then you'd have to declare your run method:
-(void)runMethod
{
//do stuff
}

backBarButtonItem doesn't call action

I would like to call an action when my backButton is clicked but this doesn't seem to be it.
viewDidLoad in rootViewController:
self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc]
initWithTitle:#"Logout"
style:UIBarButtonItemStyleDone
target:self
action:#selector(logout)] autorelease];
The title of it is correct but nothing happens.
logout (in the rootViewController)
header:
-(void)logout;
body:
-(void)logout {
NSLog(#"test");
[[User owner] logout];
}
Could anyone tell me how to solve this, since i have no idea. Thanks
You can change only the title of backBarButton. You can try to use viewWillDisappear or viewDidDisappear functions but they could be called not only after you press your button. The leftBarButton is a better solution but this button's view differs from backBarButton's view.
The backBarButtonItem exists specifically to change the appearance of the back button. If you need a custom action, you should consider using leftBarButtonItem instead.

Resources