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];
}
Related
I have a master / detail based iPhone app. Without using a tabbed navigation style application, how do I create a persistent button at the bottom of the navigation controller ( I want it on every view ). Please wireframe ( its the plus button ). I've tried adding a button bar item to the toolbar at the bottom, but for some reason it won't show up. I'm using xcode 8 and ios 10.
for reference I created a custom navigation controller class and inserted the following code
#import "MainNavigationController.h"
#interface MainNavigationController ()
#end
#implementation MainNavigationController
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(#"Testing");
// Do any additional setup after loading the view.
NSMutableArray *buttonsArray = [[NSMutableArray alloc] init];
UIBarButtonItem *myButton1=[[UIBarButtonItem alloc] initWithTitle:#"button 1" style:UIBarButtonItemStylePlain target:self action:#selector(toolbarButtonPressed1:)];
[buttonsArray addObject:myButton1];
UIBarButtonItem *myButton2 = [[UIBarButtonItem alloc] initWithTitle:#"button 2" style:UIBarButtonItemStylePlain target:self action:#selector(toolbarButtonPressed2:)];
[buttonsArray addObject:myButton2];
[self setToolbarItems:buttonsArray animated:YES];
[self.toolbar setBarStyle:UIBarStyleBlack];
[self.toolbar setItems: buttonsArray animated:NO];
}
Then I make the toolbar visible in the interface builder ... the toolbar shows and the color is set in code, but the buttons do not appear
A simple solution would be to embed your UINavigationController in a custom parent view controller. This means that the navigation controller's view is a subview of the parent view controller's view. And that means that you could add another subview of the parent view controller's view, the button.
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.
}
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;
I had two Table View Controllers and they are connected each other by "segues" in Storyboard. Then I've cut the "segues" and insert another controller. And connection now is not through "segues". To connect the views I'm using the code:
UIStoryboard *storyboard = self.storyboard;
OptionsViewController *options = [storyboard instantiateViewControllerWithIdentifier:#"OptionsViewController"]
[self.navigationController pushViewController:options animated:YES];
And now when I'm clicking a "Back" button in navigation bar my program crashes. How to fix it?
Console shows (After view loaded, before I've pressed the "Back" button.): "nested push animation can result in corrupted navigation bar"
"Finishing up a navigation transition in an unexpected state. Navigation Bar subview tree might get corrupted."
After I've pressed the "Back" button: "terminating with uncaught exception of type NSException"
Navigation Controller is the initial controller in my storyboard, but the Views are not connected each other.
FIXED: I've just set the "animated:NO" at the end of my code.
Check that to calling pushViewController before viewDidAppear is not safe. So you should create your code according to that.
Try modifying line to
[self.navigationController pushViewController:options animated:YES];
Update
If you don't care about normal back button, try below code.
- (void) viewDidLoad
{
// ADD BELOW CODE IN viewDidLoad ALONG WITH REST OF YOUR CODE
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#”back”
style:UIBarButtonItemStyleBordered target:self action:#selector(backBarButton:)];
self.navigationItem.leftBarButtonItem = backButton;
}
- (void) backBarButton:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}
You can play around with style to set your desired button style.