uiNavigationController Error when pushing view controller [duplicate] - iphone

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
iphone - “Pushing the same view controller instance more than once is not supported” exception
I am properly pushing test2ViewController from 1 as follows,
self.controller2 = [[test2ViewController alloc] initWithNibName:#"test2ViewController" bundle:nil anUser:self.idUser];
[[self navigationController] pushViewController:self.controller2 animated:NO];
[self.controller2 release];
from 2 to 1 I pop it after initialize 1 again (necessary to initialize).
self.controller1 = [[test1ViewController alloc] initWithNibName:#"test1ViewController" bundle:nil anUser:self.idUser];
[[self navigationController] popToRootViewControllerAnimated:NO];
[self.controller1 release];
and problem appers when trying to push again 2 from 1, app crashes with an error,
Pushing the same view controller instance more than once is not supported
what am doing wrong? thank you.

Well first off you are creating another instance of test2ViewController so you will go to a different instance each time you change view.
What you should do:
if(!test2ViewController)
secondView = [[test2ViewController alloc] init...];
[self navigationController pushViewController:secondView animated:NO];
and to return, simply:
[self.navigationController popViewControllerAnimated:NO];
PoppingtoRoot causes you to pop to the very first view controller that used the pushViewController method.

Judging by the code you posted you only push one view controller (controller2) to your navigation controller.
popToRootViewControllerAnimated: will remove all view controllers from the stack except the root view controller (which seems to be controller2 in your case). So basically it does nothing.
Then you try to push the same view controller2 again and it fails, because as the error message said, that's not allowed.

You do not need to reinitialize viewController1 again; if you are pushing the viewController2 from 1, than you only have to invoke
[self.navigationController popToRootViewControllerAnimated:NO];
because viewController1 is already in the stack. What this method will do is to remove all viewControllers on the stack except the first one and move back to it.
If viewController1 is not the rootView controller you should use
[self.navigationController popViewControllerAnimated:NO];
which will pop only the last pushed viewcontroller on the stack and reveal the one beneath it.

Related

presentmodalviewcontroller hides UITabBar

I have a basic doubt.I need to push viewController to another viewController .
and I am trying with code
Display1 *ac =[[Display1 alloc]init];
[[self navigationController]pushViewController:ac animated:YES];
this gives the option of navigation to previous stack.
I don't option to move on Previous Stack .for that reason I tried presentModalViewController
Display1 *ac =[[Display1 alloc]init];
[self presentModalViewController:ac animated:YES];
But this works fine this one does not give me option to but presentModalViewController Hides my UITabBarController
.is there anyway to, display UITabBarController with presentModalViewController.
or using pushViewController to not display previous stack
Darshana Is right If you dont want back option that use
self.navigationItem.hidesBackButton = YES;
before
[[self navigationController]pushViewController:ac animated:YES];
But if you want UITabBar at new UIViewController than you have to add that controller like this:
NextViewController *nextViewController=[[NextViewController alloc]initWithNibName:#"NextViewController" bundle:nil];
UINavigationController *navBar=[[UINavigationController alloc]initWithRootViewController:nextViewController];
[self.navigationController presentModalViewController:navBar animated:YES];
I have take this from PresentModalViewController not showing navigation bar on next view
But first decide what you want to use PUSH OR MODAL.
this both has different purpose.

don't understand how to use navigation controller in iphone

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];
}

viewDidDisapper won't call

I have uinavigation controller with a view controller named VC1,now i have a button in VC1 that when i click on him i go to view controller VC2,this is how i bring VC2 to screen :
VC2 *tmp = [[VC2 alloc]init];
[[self navigationController] pushViewController:tmp animated:YES];
[tmp release];
now when i click in VC2 on the back button of the navigation to return to VC1 its work but i put in VC2 viewDidDisappear and viewWillDisappear methods,and when i click on the back button this function won't called, any one have any idea?
You may call the view[..] methods manually from the UINavigationControllerDelegate callbacks, however the easiest way to ensure that the methods are called by the super implementation of the UINavigationController is just to call them manually once when the UINavigationController is allocated.
See my answer here: iPhone viewWillAppear not firing
So immediately after you have called navController = [[UINavigationController alloc] init.., make sure to call
[navController viewWillAppear:NO];
[navController viewDidAppear:NO];
[navController viewWillDisappear:NO];
[navController viewDidDisappear:NO];
This should ensure events are correctly forwarded to each view controller in the future.
If you want to call the viewWillDisappear/viewDidDisappear methods, your view controller has to do that manually before popping itself off the nav stack. Have a look at this
,you might get an idea how to do it.

presentmodalviewcontroller navigationcontroller

I am creating a Navigation based iPhone application.
In that I have called a UiViewController using presentModalViewController. After that, the ViewController becomes visible. From that ViewController I need to call another ViewController using the sample presentModalViewController. Is this possible or not?
What do you mean by "call another uiviewcontroller"? (It really helps if you can be more detailed in your question.) If you mean, "slide in another view controller", then:
MyNewViewController *myNewViewController = [[MyNewViewController alloc] initWithNibName:#"MyNewViewController" bundle:nil];
[navigationController pushViewController:myNewViewController animated:YES];
[myNewViewController release];
...where:
MyNewViewController is the new view controller class that you want to slide in (the above code assumes you have an XIB file for the view controller class).
navigationController points to the current navigation controller. You'll have to replace it with something like [self navigationController], depending where you are in the view hierarchy.
U might be using following line to present a view controller.
//assume name of viewController which u want to present is "myViewController"
[self.navigationController presentModalViewController:myViewController animated:YES]
If you want to push an other ViewController or present an other ViewController then u will need to replace above line with following lines.
//[self.navigationController presentModalViewController:myViewController animated:YES];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:myViewController];
navigationController.navigationBarHidden = YES; //if u want to show navigation bar then remove this line
[self presentModalViewController:navigationController animated:YES];
After using above code you can present or push other view controllers within presented view controller.
Hope it will solve your problem :)

UINavigationController popViewController is popping everything!

So I'm trying to pop a view controller off the stack when an error occurs, but it seems like it's popping too much off in one go. The navigation bar up the top loses its title and buttons, but the old table view data remains visible. I have no idea what's going on...
The basic set up is:
Tab View template
Navigation controller
View controller (Loaded from the xib)
View controller (Pushed, what I want to pop)
Here's the code:
NSLog(#"%#", [[self navigationController] viewControllers]);
[[self navigationController] popViewControllerAnimated:NO];
NSLog(#"%#", [[self navigationController] viewControllers]);
The resulting NSLog's show:
2009-09-22 19:57:14.115 App[34707:550b] (
<MyViewController: 0xd38a70>,
<MyViewController: 0xd36b50>
)
2009-09-22 19:57:14.115 App[34707:550b] (null)
Anyone have experience with this?
I am seeing some odd UINavigationController stack behavior with just using
[self.navigationController popViewControllerAnimated:YES];
inside of a delegate called out of a tableView:didSelectRowAtIndexPath: call.
The views don't work right, and a UINavigationController provided "back" operation doesn't correctly pop the view of this delegate, going back another level.
I found that if I used
[self.navigationController popToViewController:self animated:YES];
instead, that suddenly everything worked fine. This is in an application with ARC turned on.
So, I can only guess that there is some reference housekeeping that doesn't happen correctly unless you tell it to pop back to a specific view controller when you will pop a view controller that will immediately become "unreferenced" by that pop.
I fixed it. The code that was popping the view was getting called in viewDidLoad. This meant it was getting popped before the view had actually animated in completely.
I moved that code to viewDidAppear and now it works as advertised.
[[self navigationController] popViewControllerAnimated:NO];
//here you pop **self** from navigation controller. And now
[self navigationController] == nil;
// And
[nil viewControllers] == nil
Try to do this:
UINavigationController *nc = [self navigationController];
NSLog(#"%#", [nc viewControllers]);
[nc popViewControllerAnimated:NO];
NSLog(#"%#", [nc viewControllers]);
What is wrong with?
[self.navigationController popViewControllerAnimated:NO];
Also, you may want to check how you push the ViewController onto the stack. Something doesn't sound right.