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.
}
Related
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
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
I am trying to add a bar button to my iOS app and can't get it to show up. I can see the bar in the Navigation Item's view hierarchy by setting a breakpoint. If it helps, I chose 'Embed Navigation Controller'. Any idea what's going on?
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(choosePreferredTerm:)];
[self.navItem setRightBarButtonItem:item animated:YES];
Here is the connection in IB
This is what the embedded Navigation Controller looks like:
This is what it looks like on the sim:
Try this rather than using custom outlet "navItem" also insure your application has a navigation controller in place
self.navigationItem.rightBarButtonItem = barButton;
My app layout is as follows -
the rootViewController is a tabViewController with 3 tabs each having a UINavigationController as their rootViewController. Within one of these tabs I am pushing upon cell selection to another tabController which now has two tabs. What I am trying to do is set the rightBarButtonItem on each of these two tab's viewControllers... in the viewDidLoad method of both of these I am doing:
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:#selector(selectionChanged:)];
however this is doing absolutely nothing! I thought from the apple documentations that you could set the navigationItem's rightBarButtonItem from anywhere within your navigation controllers view hierarchy but that doesn't seem to be the case here. Any idea what - if anything - I am doing wrong?
The solution to this is to instead of simply setting the rightBarButtonItem on self.navigationItem we need to set it on the parent tabBarController like so :
self.tabBarController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:#selector(selectionChanged:)];
I am using xcode 4.4.1 using the IOS 5.1 simulator. I have started off with a View Controller that has 2 buttons. I have one button connected to a Table view that is embedded with a navigation controller. It is connected using a modal segue. When I run it in the simulator and click the button it goes to the table view but I don't have a button at the top to get back to the view controller. How do I make that button appear?
You have to make a UIBarButtonItem and set it to the navigationItem's leftBarButtonItem or rightBarButtonItem, somewhat like the following:
UIBarButtonItem *closeBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Close" style:UIBarButtonItemStyleBordered target:self selector:#selector(closeTableView)];
self.navigationItem.rightBarButtonItem = closeBarButtonItem;
Then in the closeTableView method:
- (void)closeTableView
{
[self dismissViewControllerAnimated:YES completion:nil];
}