navigated from one view to another will and when i pop my view back viewWillAppear method is not calling.
can you please let me know the reason.
When you load View the first time viewWillAppear method is called? try to use
[[self navigationController] popViewControllerAnimated:TRUE];
to return back
It's typical that these problems are caused by improper use of view controller containment. You need to look at your view controller hierarchy and research the following methods:
[UIViewController addChildViewController:];
[UIViewController removeFromParentViewController:];
[UIViewController transitionFromViewController:toViewController:duration:options:animations:completion:]
[UIViewController willMoveToParentViewController:]
[UIViewController didMoveToParentViewController:]
Read the Implementing a Container View Controller section of the UIViewController Class Reference.
If u are doing all well than ur application is crashed some where, use break point and check , even you can check and follow these steps......
step 1. for push in FirstViewController
SecondViewController *second = [[SecondViewController alloc]init]
[self.navigationController pushViewController:second animated:TRUE];
Step 2. for pop in SecondViewController
//check navigation controller exist in ur application stack
NSArray *arrView = [self.navigationController viewControllers];
NSLog(#"arrView %#",arrView);
for(int i = 0; i <[arrView count]-1, i++)
{
if([arrView objectAtIndex:i] isKindOfClass:[FirstViewController class])
{
[self.navigationController popViewControllerAnimated:TRUE];
}
}
I've also met this situation.
In iOS 5, it's ok.
But when I test in iOS 4.3.1, viewWillAppear is not calling.
So you need call viewWillAppear manually
You can try this
[[self.navigationController.viewControllers objectAtIndex:0] viewWillAppear:YES];
[self.navigationController popViewControllerAnimated:YES];
Related
In our iOS application with three UIViewControllers one after another, we would like to skip the middle one based on some condition and go directly from first to third. However, the user should be able to come back to the second one via the "Back" button on the third controller.
I tried [self performSegueWithIdentifier:#"segueId" sender:sender]; from viewDidLoad, viewWillAppear but this corrupts the navigation bar as indicated by the debug log. Calling this code from viewDidAppear works fine, but then the second view is already displayed, which is what I was trying to avoid in the first place.
I also tried [self.navigationController pushViewController:vc animated:NO]; but the result is similarly corrupted nav bar, even though this time debug log does not have such entries.
What would be the supported way of doing this (if it is at all possible)?
The target is iPhone4 with iOS 5.1, and the dev environment is Xcode 4.3.
I use the following code in an app. Works exactly as expected.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
SecondViewController *secondVC = [self.storyboard instantiateViewControllerWithIdentifier:#"DetailViewController"];
if (indexPath.row == 0) {
// skip second vc
ThirdViewController *thirdVC = [self.storyboard instantiateViewControllerWithIdentifier:#"ThirdViewControllerViewController"];
[self.navigationController pushViewController:secondVC animated:NO];
[self.navigationController pushViewController:thirdVC animated:YES];
}
else {
// push second vc
[self.navigationController pushViewController:secondVC animated:YES];
}
}
If you want to skip a view controller you can just call UINavigationController setViewControllers:animated: It will animate to the last controller in the supplied array, and the user will be able to "back" out of that stack.
You can build up the array of view controllers any way you like; perhaps starting with the existing array of view controllers:
NSMutableArray* newViewControllers = [[navController.viewcontrollers mutablecopy] autorelease];
[newViewControllers addObject: ...];
[navController setViewControllers: newViewControllers animated: YES];
I have a UIViewController class and a second which I want to push modally preferably. However I can't seem to call [self pushModalViewController:...], how come?
What requirements do I need to meet to be able to do so?
I am doing this and getting a black view pushed:
vc = [[ViewController alloc] init];
[vc setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentModalViewController:vc animated:YES];
I have made my view controller in my storyboard and given it a custom class. I am trying to present this view modally via this class as seen in my code.
Any help much appreciated, thanks.
'push' and 'modal' don't belong together in the same thought. You can:
present a modal view controller, preferably using -presentViewController:animated:completion:, which is the modern replacement for -presentModalViewController:animated:
push a view controller onto the navigation stack, assuming that you're using a UINavigationController. To do that from a view controller, use:
[self.navigationController pushViewController:foo animated:YES];
You're actually looking for [self presentModalViewController:myViewController.view animated:YES]
How are you initing this viewcontroller. From the posted code, I assume it is being initialized to a blank view.
Maybe you can init it from a nibname or something?
vc = [[ViewController alloc] initWithNibName:#"NibName" bundle:nil];
[vc setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentModalViewController:vc animated:YES];
Also another thing to note, if this is a custom class and no nib file, is there any code in initWithCoder? How does the viewDidLoad looks like?
I'm having a little predicament switching between views, here.
Alright, so, I have this view controller class in my iPhone project called "BaseViewController," which is the default, which has a button called "GoToNextView." I added another ViewController to the storyboard called "NextViewController," and then I created another custom view controller class called "NextViewController." Under the inspector window for NextViewController on the storyboard I changed its custom class to "NextViewController;" I'm assuming everything should be hooked up, now. When I click on the "GoToNextView" button, though, the application stops with a SIGABRT message.
Here's the code for my button click action in the BaseViewController class.
- (IBAction)Transition_Next:(id)sender
{
nextViewController = [[NextViewController alloc]
initWithNibName:#"SecondView"
bundle:[NSBundle mainBundle]];
[self.view addSubview:nextViewController.view];
}
What might I be doing wrong, here?
Thanks in advance...
You can use:
- (IBAction)Transition_Next:(id)sender
nextViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:nextViewController animated:YES];
}
Instead of your code and it should work! Hope that helps!
You can use [self.navigationController pushViewController:nextViewController animated:NO]; or if you don't have a navigation controller, you may use
[self presentModalViewController:nextViewController animated:NO]; instead of [self.view addSubview:nextViewController.view];
I'm doing an application which uses a UINavigationController and I'm switching to other UIViewControllers as follows:
if(self.myViewController == nil){
MyViewController *aViewController = [[MyViewController alloc] initWithNibName:#"MyViewController" bundle:nil];
self.myViewController = aViewController;
[aViewController release];
}
AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
[delegate.myNavController pushViewController:myViewController animated:YES];
I imagine this is creating a pile of UIViewControllers into the UINavigationController, maybe an array of indexs? I would like to know how to turn back without having to be back one by one.
For example, I'm sailing through a few screens and with a button I would like to return at the first index of navigation. I would also like know how to modify indexes, view, erase and anything pertaining to this issue.
Sorry if I have not explained well.
You've asked two questions.
The first is, how do I get back to my first view controller. As #Thomas Clayson and #ender have answered, you want the popToRootViewControllerAnimated: method of your navigationcontroller object for that.
The second is how to move to a particular index in the view controller stack. The answer to that is, you can set the array of viewControllers explicitly. So you can pull out the current listing of view controllers, modify it, and set it back into the navigationController stack. It'll reset the stack and animate you moving to the top item in the stack.
Thusly:
NSMutableArray *controllers = self.navigationController.viewControllers;
[controllers removeObjectAtIndex:[controllers count] - 1]; //or whatever
[self.navigationController setViewControllers:controllers animated:YES];
NSArray *viewControllers = [[self navigationController] viewControllers];
for (int i = 0; i < [viewContrlls count]; i++){
id obj = [viewControllers objectAtIndex:i];
if ([obj isKindOfClass:[yourViewControllername class]]){
[[self navigationController] popToViewController:obj animated:YES];
return;
}
}
Using this you can come back to any specified viewController.
[self.navigationController popToRootViewControllerAnimated:YES];
Will take you back to the very first view controller (root view controller).
Hope this helps
Use this
NSArray *viewContrlls=[[NSArray alloc] initWithArray:[[self navigationController] viewControllers]];
id obj=[viewContrlls objectAtIndex:1];
[[self navigationController] popToViewController:obj animated:YES];
[viewContrlls release];
You should use popToRootViewControllerAnimated: From UINavigationController class reference:
Pops all the view controllers on the stack except the root view
controller and updates the display.
You can return to the first view with
[self.navigationController popToRootViewControllerAnimated:YES];
That being said, you can also remove a particular view controller, or navigate to a specific index in your view controller if you look at the example.
NSMutableArray *allViewControllers = [NSMutableArray arrayWithArray:navigationController.viewControllers];
// You can now manipulate this array with the methods used for NSMutableArray to find out / perform actions on the navigation stack
[allViewControllers removeObjectIdenticalTo: removedViewController];
// You can remove a specific view controller with this.
navigationController.viewControllers = allViewControllers;
I have an issue, viewWillAppear method in the UIViewController which is added to the screen by presentModalViewController method does not "go into" viewWillAppear method. Only time this method is invoked is together with the viewDidLoad, the first time.
- (IBAction)openModal:(id)sender {
if (self.nvc == nil) {
ModalViewController *vc = [[ModalViewController alloc] init];
self.nvc = vc;
[vc release];
}
self.nvc.segmentedIndex = [[self.navigationController.viewControllers objectAtIndex:0] index];
[self presentModalViewController:self.nvc animated:YES];
}
The property is of type retain.
Thanks in advance.
right, it works different and it now goes into viewWillAppear only once. So if you want to change the appearance of your view after dismissing a modal view you should do it using a modal delegate pattern. See the link:
http://useyourloaf.com/blog/2010/5/3/ipad-modal-view-controllers.html
Though it's for iPad, but you can get the idea. In the latest iOS versions it works the same.
Hope it helps