In my delegate class I have this -
BluenibViewController *mvc = [[BluenibViewController alloc] init];
UINavigationController *unvc = [[UINavigationController alloc] init];
[unvc pushViewController:mvc animated:NO];
[mvc release];
[self.window addSubview:unvc.view];
[self.window makeKeyAndVisible];
return YES;
and in my BluenibViewController I have this method -
-(IBAction) BookingsViewController:(id)sender
{
bookingsViewController1 = [[BookingsViewController alloc]
initWithNibName:#"BookingsViewController"
bundle:nil];
UINavigationController *navController = self.navigationController;
[navController pushViewController:bookingsViewController1 animated:YES];
self.title = #"Bookings";
[self.window makeKeyAndVisible];
[self.view addSubview:bookingsViewController1.view];
[navController release];
[bookingsViewController1 release];
}
Ob click of this method I am able to go to the next view but there is no back button on the navigation bar.
Please point me to the silly mistake I am doing.
I see quite a few errors here. Lets see if I can't be of some service.
First, I see that you're overreleasing your navigation controller [navController release];
Second, you should only have to make the window key/visible once in your entire project. You set your navController as the rootViewController for your window, make it key/visible, and then you should never have to do anything with your window again. Don't think it's breaking anything, but you should remove all calls to make key/visible beyond the first.
In the end, pushing a new view controller should look like this:
[self.navigationController pushViewController:[[[BookingsViewController alloc] init] autorelease] animated:YES];
You shouldn't need anything else to get what you're looking for.
You should not add unvc.view as a subview of self.window. You should just assign unvc as self.window.rootViewController:
self.window.rootViewController = unvc;
and you should not add bookingsViewController1.view as a subview of self.view (in your BookingsViewController: method). The navigation controller will take care of getting bookingsViewController1.view onto the screen after you have pushed it.
self.navigationItem.hidesBackButton =YES;
Follow this code . Hope it helps ....
it comes a little bit late but in iOS 8 you can do:
[self.navigationController pushViewController:secondViewController animated:YES];
Related
So I am trying to create an iphone app and I am trying to set up the navigation controller. I got it to work from one view to another and then back.(code below) Is there a way to do it for multiple views? For instance, starting from the beginning pressing one button that changes to another view then pressing another button that goes to another different view. How would I implement the navigation controller to work without using storyboard and actually programming it? Any tips would be appreciated thanks.
This is in AppDelegate:
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
LoginViewController *loginController = [[LoginViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:loginController];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
return YES;
This is in one of the button methods in one of the views:
[self.navigationController pushViewController:mainViewController animated:YES];
That is what the navigation controller do.
You can navigate to multiple views like this;
To navigate from main view to secondViewController
[self.navigationController pushViewController:secondViewController animated:YES];
The current navigation stack is mainViewcontroller -> secondViewController
From second View to third View
[self.navigationController pushViewController:thirdViewController animated:YES];
The current navigation stack is mainViewcontroller -> secondViewController - >thirdViewController
To go back to previous view you can call
[self popViewControllerAnimated:YES];
for 1 to 2 view
[self.navigationController pushViewController:secondViewController animated:YES];
from 2 to 3 view
[self.navigationController pushViewController:thirdViewController animated:YES];
back from 3 to 2 view
[self popViewControllerAnimated:YES];
With the following code:
MyModalViewController *mMVC = [[MyModalViewController alloc] init];
UINavigationController *mMNavVC = [[UINavigationController alloc] initWithRootViewController:mMNavVC];
mMNavVC.navigationBar.barStyle = UIBarStyleBlackOpaque;
[[[appdelegate window] rootViewController] presentViewController:mMNavVC animated:YES completion:nil];
[mMVC release];
[mMNavVC release];
//(Yes we are not using ARC yet - kills me)
the view presents but it does not load before fully sliding up to the top. The Nav controller does load properly and you see it slide all the way up. However it's just a frame. In other words, you can see the presenting view controller as the nav controller slides up into place - THEN the mMVC loads.
Thanks for the help!
Your problem is that you are trying to create a UINavigationController while using it as the UINavigationController rootviewcontroller. Fixing this line :
UINavigationController *mMNavVC = [[UINavigationController alloc]
initWithRootViewController:mMNavVC];
to this :
MyModalViewController *mMVC = [[MyModalViewController alloc] init];
UINavigationController *mMNavVC = [[UINavigationController alloc] initWithRootViewController:mMVC];
will load the modal view controller before showing it.
MyModalViewController *mMVC = [[MyModalViewController alloc] init];
UINavigationController *mMNavVC = [[UINavigationController alloc] initWithRootViewController:mMNavVC];
mMNavVC.navigationBar.barStyle = UIBarStyleBlackOpaque;
[[[app window] rootViewController].navigationController pushViewController: mMNavVC animated:YES];
[mMVC release];
[mMNavVC release];
Try adding a line where you access that vc's view, i.e.
MyModalViewController *mMVC = [[MyModalViewController alloc] init];
UINavigationController *mMNavVC = [[UINavigationController alloc] initWithRootViewController:mMNavVC];
mMNavVC.navigationBar.barStyle = UIBarStyleBlackOpaque;
mMVC.view.backgroundColor = [UIColor whiteColor];
[[[appdelegate window] rootViewController] presentViewController:mMNavVC animated:YES completion:nil];
[mMVC release];
[mMNavVC release];
This is kind of a workaround for a problem that your viewController's view doesn't get loaded before navigationController's viewDidAppear method is called. If what I posted doesn't work, the problem is elsewhere.
Just try it and tell if it's ok now. :)
OK. Everybody gets a 1up for their answers because they were all helpful to me.
LucOlivierDB gets the check mark because that was just a stupid thing for me to not have seen.
!!NOTE:!! As it turns out my real problem as this line of code:
self.view.backgroundColor = [UIColor clearColor];
Someone thought it would be clever to have all the views inherit the background by making the view transparent. So when it drew the view it was drawing it transparent. Since the was not considered done drawing until the modal action was done you could see the view beneath it before that.
Thanks guys. You call made me think and learn!
i'm new to iphone development and i need some guidance in pushing views. I have the following application scenario "press on button ->switch view (contains a table)->press on table cell ->switch view".
To switch when i press the button i use the following code :
MapView *screen = [[MapView alloc] initWithNibName:nil bundle:nil];
screen.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:screen animated:YES];
[screen release];
Then to push another view i want to use this code (but it does not work - and im asking why?)
Newclass *nextController = [[Newclass alloc] initWithItem:theItem];
[self.navigationController pushViewController:nextController animated:YES];
[nextController release];
You want to push this view from your MapView? Looks like you don't have a UINavigationController which is needed to push view controllers like that.
Update the first part of your code to put the MapView inside UINavigationController, than you can push other views onto it with that second part of code you have.
MapView *screen = [[MapView alloc] initWithNibName:nil bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:screen]; // this puts the MapView onto navigation controller
navController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; // you will present the navController instead of screen so change it's modalTransitionStyle
[self presentModalViewController:navController animated:YES]; // present it
[screen release];
[navController release]; // don't forget to release
I was wondering how I could load a different XIB when my application initially loads. Right now when my application loads I just get a black screen because there is no MainWindow.xib. Ideally I want to load a XIB that has a UINavigationController inside of it. Does anyone know how to do this? Thanks in advance!
In applicationDidFinishLaunching you may think to something like
UINavigationController* navigationController = [[UINavigationController alloc] init];
UIViewController *controller = [[UIViewController alloc] initWithNibName:#"First" bundle:nil];
[ navigationController pushViewController:controller animated:NO];
[controller release];
[window addSubview:navigationController.view];
[window makeKeyAndVisible];
Of course, it actually depends on what you are planning to do...
i was searching this whole afternoon in hope of solution but i didn't manage to find anything helping me solve the problem.
The problem is, i have no intention of using UINavigationController until a button gets taped, and after that i would like to present view controller containing a navigationcontroller, or reverse, modally. in my search i came across this solution:
MyExampleViewController *vc = [[[MyExampleViewController alloc] initWithNibName:#"MyExample" bundle:nil] autorelease];
UINavigationController *navController = [[[UINavigationController alloc] initWithRootViewController:vc] autorelease];
[self presentModalViewController:navController animated:YES];
which i then used it in my code and found out it is not working, to make sure i started a practice project from viewbased project templet and added this to the first view controller
- (IBAction) buttonGotPreseed{
testino *testController = [[testino alloc] initWithNibName:#"testino" bundle:nil];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:testController];
testController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:testController animated:YES];
[nav release];
[testController release];
}
didn't work also, im not seeing the navigation bar up there
any help would be greatly appreciated
You're pushing the root view controller, not the navigation controller itself. Line 5 should be:
[self presentModalViewController:nav animated:YES];