A very simple question: My iPhone app has a button in MainWindow.xib. When I press that button a new view should load. That view will contain a nice navigation controller. How can I do that?
All the information I've found is about apps that start directly from a navigation controller. I need to load the nav controller after a button click.
Many thanks!
Another way to go about this is to simply hide the navigation bar in your root controller:
- (void) viewDidLoad {
...
if (![self.navigationController isNavigationBarHidden])
[self.navigationController setNavigationBarHidden:YES animated:NO];
...
}
That way, you have a nice, clean root controller with no navigation bar in the way.
When you click on a button in your root controller, you simply push in a new view and un-hide the navigation bar:
- (IBAction) pushAnotherView:(id)sender {
AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:#"AnotherViewController" bundle:nil];
[self.navigationController pushViewController:anotherViewController animated:YES];
if ([self.navigationController isNavigationBarHidden])
[self.navigationController setNavigationBarHidden:NO animated:YES];
[anotherViewController release];
}
If you have some notification or other action that brings you back to the root view controller, just hide the notification bar again:
- (void) viewWillAppear:(BOOL)animated {
if (![self.navigationController isNavigationBarHidden])
[self.navigationController setNavigationBarHidden:YES animated:YES];
[super viewWillAppear:animated];
}
Related
I have an app where the first screen (the menu for the app) does not need a navigation bar BUT the rest of the app does.
The code I am using works fine in the sense that the navigation bar is not present on the menu screen and is present elsewhere in the app BUT the BIG PROBLEM is that once you go back to the menu the navigation bar appears for about a split second and then disappears.
That is NOT a very smooth transition.
How do I make the transition SMOOTHER so that the navigation bar DOESN'T even appear for a second when I go back to the menu screen?
Here is the code that I am using:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
[self.navigationController setNavigationBarHidden:YES animated:NO];
}
return self;
}
- (void)viewDidLoad {
[self.navigationController setNavigationBarHidden:YES animated:NO];
[super viewDidLoad];
}
-(void) viewDidAppear: (BOOL)animated {
[[self navigationController] setNavigationBarHidden:YES animated:NO];
[super viewDidAppear:animated];
}
Try like this,
-(void) viewWillAppear: (BOOL)animated {
[super viewWillAppear:animated];
[[self navigationController] setNavigationBarHidden:YES animated:YES];
}
Hope it may helps you...
In your first view controller:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES animated:YES];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
In your second view controller (not needed, but good practice for code clarity) :
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
set this in the view did disappear in first screen of the app
[[self navigationController] setNavigationBarHidden:NO animated:YES];
and this in viewdiddisappear of the second VC
[self.navigationController setNavigationBarHidden:YES animated:NO];
Instead of hiding and showing the navigation bar, you can update the alpha for the navigation bar. It will animate smoothly during the transition. For the view controller with transparent nav bar, instead of modifying the nav bar, create a navbar (or just the back button and title etc.) manually in the second controller's view. We will then hide the navbar when transitioning from first view controller to the second one.
On your second controller's viewWillDisappear and on your first view controller's viewWillAppear:, set the navigation bar alpha to zero using self.navigationController.navigationBar.alpha = 0;. Since this is in animation block, this will make the navigation bar disappear during the push animation.
Set the alpha back to one in second controller's viewWillAppear and first controller viewWillDisappear.
I am new in iPhone development. I created an app which use a navigation bar using storyboard. My problem is that i am opening a viewB programmatically from viewA on button click and it successful. Now to go back to viewA i have used cancel button. when i click on cancel button (previous) the (viewA) is opened but navigation bar is not shown. and viewA have navigation bar control but viewB does not.
Thanks in advance
View A
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
EditViewController *viewController = (EditViewController *)[storyboard instantiateViewControllerWithIdentifier:#"EditViewController"];
[self presentViewController:viewController animated:NO completion:NULL];
View B:
- (IBAction)cancelButtonPressed:(id)sender {
if ( lables != NULL) {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
ScannerViewController *viewController = (ScannerViewController *)[storyboard instantiateViewControllerWithIdentifier:#"ScannerViewController"];
[self presentViewController:viewController animated:NO completion:NULL];
}
else{
[self.navigationController popViewControllerAnimated:YES];
}
You are presenting the viewB & popping it using self.navigationController, you should use one way, either use presentviewcontroller & dismissviewcontroller.
[self dismissViewControllerAnimated:YES completion:nil];
For your scenario it is best to use UINavigationController
e.g
Pushing:
[self.navigationController pushViewController:viewController animated:YES];
Closing
[self.navigationController popViewControllerAnimated:YES];
You have to use a navigation controller, in your StoryBoard select your view A, Editor Menu > Embed In > Navigation controller.
http://developer.apple.com/library/ios/#documentation/WindowsViews/Conceptual/ViewControllerCatalog/Chapters/NavigationControllers.html
If you are using storyboard no need to present the viewController programmatically. press and hold ctrl, drag from viewCA to viewCB and select model from popup menu.
For dismissing the viewController
[self dismissViewControllerAnimated:YES completion:nil];
If you want to send any data to viewCB give segue identifier (for example "segid")
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([[segue identifier] isEqualToString:#"segid"])
{
//write your code here.
}
}
This method will get called automatically (delegate) while presenting (of pushing) viewController.
I was also facing the same problem. The solution is to never select the modal on click on the back button, because modal covers your whole view and that's why on the back screen navigation controller is not showing. So don't make connection for back button, just write the code for back button.
[self dismissViewControllerAnimated:YES completion:nil];
When you needed (e.g. viewDidAppear method) use,
[self.navigationController setNavigationBarHidden:NO animated:YES];
I want to make a back button from a modal view to the Main view.
The View Controller is embedded in the navigation controller. The menu button take me to the Second View Controller. There I have a back button who works fine using this:
[self.navigationController popViewControllerAnimated:YES];
I want to go back to the Main View Controller from the Page Two VC.
I have tried:
- (IBAction)goToRootView:(id)sender {
[self.presentingViewController dismissViewControllerAnimated:(NO) completion:nil];
[self.navigationController popViewControllerAnimated:YES];
}
and:
- (IBAction)goToRootView:(id)sender {
[self dismissViewControllerAnimated:(NO) completion:nil];
[self.navigationController popViewControllerAnimated:YES];
}
The first just goes back to the Second VC, the last sends and lldb error.
How can I go from Mantras Page Two VC to Main VC?
Thank you for your help!
You can tray this...
CHS_View_Controller *oldView = [self.storyboard instantiateViewControllerWithIdentifier:#"CHS_View"];
UINavigationController *yourNavigationController = [[UINavigationController alloc] initWithRootViewController:oldView];
yourNavigationController.modalTransitionStyle= UIModalTransitionStyleCrossDissolve;
[self presentViewController:yourNavigationController animated:YES completion:nil];
for that you must:
1) import you dest controller
#import "CHS_View_Controller.h" // your controller name
and
2)
set an Identifier for your CHS_Controller, "CHS_View" in the bellow example (into the Storyboard Editor and in the Attributes Inspector)
In the first snippet, instead of
[self.navigationController popViewControllerAnimated:YES];
try
[self.navigationController popToRootViewControllerAnimated:YES];
How to navigation to previous page!
I add navigation controller to appDelegate so thought out the application i do have navigation controller.
I hidden navigation Bar and added HeaderView on the top.
// Hidden NavigationBar
- (void) viewWillAppear:(BOOL)animated
{
[self.navigationController setNavigationBarHidden:YES animated:animated];
[super viewWillAppear:animated];
}
Now customize my header bar with UIView! I hidden navigation Bar and added HeaderView on the top.
Now i added BackButton to the header Bar. Now i want to navigation pervious page. Can any one suggest me?
#Thanks In Advance.
if the view is called by a push action, call :
[self.navigationController popViewControllerAnimated:YES];
else if the view is displaying by a modal call :
[self dismissModalViewControllerAnimated:YES];
Use [self.navigationController popviewControllerAnimated :YES];
Use this when the user taps the Back button:
[self popViewControllerAnimated:YES];
I'm extremly new to iphone and I have the following misunderstanding.
All over internet the tutorials about how to use NavigationController programatically it says:
NavigationController must be declared in applicationDidFinishLaunching and must be init with a root.After that you can add views to it.
I have this:
A UIViewController class meaning(AdiViewController.h, AdiViewController.m and AdiViewController.xib) and no Delegate file meaning no applicationDidFinishLaunching method.
What I wanna do is from my class-AdiViewController when pressing a button to go to another view.
I understand that I need a NavigationController which should retain my views having the root AdiViewController.
But my problem is where should I initializate that NavigationController in viewDidAppear??...cause I don't have the Delegate files.
If you could provide a minimal example with this small issue of mine it would be great.I'm sure that for those how are senior this is nothing but still I don't get it.Thanks
NavigationController must be declared in applicationDidFinishLaunching -> this is not true.
In your AdiViewController if you have button when you push that button you want to load navigation Controller right ?
// Hook this IBAction to your button in AdiViewController
- (IBAction)pushNavController
{
AnotherViewController* rootView = [[AnotherViewController alloc] initWithNibName:#"Anotherview" bundle:nil];
UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:rootView];
[rootView release];
[self presentModalViewController:navController animated:YES];
[navController release];
}
If you are in AnotherViewController i.e., you are in root view controller of Navigation controller. You need to push and pop view controllers from there. For example if you have a button in AnotherViewController:
// push next view controller onto navigation controller's stack
- (IBAction)pushNextViewController
{
NextViewController* nextView = [[NextViewController alloc] initWithNibName:#"NextView" bundle:nil];
[self.navigationController pushViewController:nextView animated:YES];
[nextView release];
}
// Similarly if you want to go back to AnotherViewController from NextViewController you just pop that from navigation controller's stack
- (IBAction)pushNextViewController
{
[self.navigationController popViewControllerAnimated:YES];
}