I have two views which use UINavigationController. I want the second view to "slide" with animation back to the first view when I tap on a custom button. (not the original in navigationbar)
Try something like this on that button action,
[self.navigationController popToViewController:aViewController animated:YES];
or
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:([self.navigationController.viewControllers count] - 2)] animated:YES];
Here aViewController represents the previous view controller. Similarly you can pop to any viewcontroller you want by using a similar code.
[self.navigationController popToRootViewControllerAnimated:YES]; will take you to the root view controller.
Related
I have a View that has a button in it, something like this Button---(segue)---> TableViewControllerNO1-----(segue)-->TableViewControllerNO2. In the TableViewControllerNO2 I delete all the rows by pressing a button and I want after I delete the rows to return to the first View, the one before the TableViewControllerNO1, how can I dismiss the two TableViewControllers from TableViewControllerNO2?
You have a cool method to do this: popToViewController
[self.navigationController popToViewController:viewController animated:YES];
So the important is inject a reference to that viewController.
Otherwise you can also do:
[self.navigationController popViewControllerAnimated:NO];
[self.navigationController popViewControllerAnimated:YES];
but the right way is the first.
You should use - (NSArray *)popToRootViewControllerAnimated:(BOOL)animated (assuming you are using a navigation controller). This would take you all the way back to the root view controller in your navigation stack, and use the standard animation.
I am building an application in which, i have 3 ViewControllers.
Also i have created custom navigation controller.
A-ViewController -> This contains 2 buttons, 1st button is for opening B-ViewController & 2nd button is for C-ViewController.
From 1st button, I am using pushviewcontroller for B-ViewController, means B-ViewController is pushed from A-ViewController.
From 2nd button C-ViewController is presented using presentviewcontroller.
Now on pressing back button in both B & C ViewControllers, I have to use pop view controller in B-ViewController & dismiss in C-ViewController.
As currently i know the pages, but there should be a generic solution.
Is there any way to identify whether current navigation controller is pushed or popped.
As there are some pages which can be pushed or presented, but i dont want to set any bool variable. I need use the support from apple.
NSArray *arrViewControllers = [[AppDelegate sharedInstance].navigationController viewControllers];
NSLog(#"[arrViewControllers count] = %d",[arrViewControllers count]);
I am using the above code for fetching the list of view controllers in the stack.
But i am not able to identify whether it is pushed or presented.
Can anybody help me in this ?
You can check if your view controller exists in the navigation view controller's stack. Simply check it as
if([self.navigationController topViewController] == self){
//VC is the top most view controller
[self.navigationController popViewControllerAnimated:YES];
}else{
//You can put some checks here to be dead sure its a modally presented view controller
[self dismissViewControllerAnimated:YES completion:nil];
}
This is the code for Finding the navigation controller is presented or pushed from previous controller.
NSArray *arrViewControllers = [[AppDelegate sharedInstance].navigationController viewControllers];
UIViewController *viewController = [arrViewControllers lastObject];
if (viewController.presentedViewController)
{
[self dismissViewControllerAnimated:YES completion:nil];
}
else{
[self.navigationController popViewControllerAnimated:YES];
}
I have a storyboard app that has a few view controller that need to get pushed on the navigation stack programmatically, instead of with segues. I'm using the following code to do it:
POCollectionViewController* vc = [self.storyboard instantiateViewControllerWithIdentifier:#"categoryView"];
POCategory* destination = (POCategory*)self.collections[indexPath.row];
[vc setShowingCollection:destination];
[self.navigationController pushViewController:vc animated:YES];
My problem is that when I use segues to push view controllers and I press the 'back' button, the scroll position is preserved, but when I use the above code and press the 'back' button, my previous view controller appears with the scroll bar at the top.
Is it re-instantiating a view controller that is supposed to be already on the navigation stack? How can I preserve the scroll position?
EDIT
Code for setShowingCollection:
-(void)setShowingCollection:(POCategory*)collection
{
self->showingCollection = collection;
self.title = collection.name;
self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
self.HUD.delegate = self;
[self.HUD showWhileExecuting:#selector(buildDisplayItems) onTarget:self withObject:nil animated:YES];
}
The buildDisplayItems method fetches a bunch of the data from the server based on the showing collection's ID.
I think that you are not popping the controller correctly, your are not poping to the pushed controller, but popping to a newest controller. How you are doing the pop controller in your back button ?
you should do like this :
[self.navigationController popViewControllerAnimated:YES];
My immediate thought is if you must instantiate a new controller for every "segue" you will have to save the scroll position and when you reload the view cause the the uiscrollview to scroll.
Something like this saved in a property should do the trick:
CGRect scrolledRect = CGRectMake(scrollView.contentOffset.x, scrollView.contentOffset.y, scrollView.bounds.size.width, scrollView.bounds.size.height);
Then when you reload:
[scrollView scrollRectToVisible:scrolledRect animated:NO];
I need to jump between view controllers. For example:
View1: First screen (Just logo)
View2: Download Screen
View3: First app screen (Some Buttons)
View4-View(N): some app screens
When user enters app the app goes to View1->View2 (downloads stuff)->View 3->View4->View5
Then user wish to go to First app screen (View3) he does:
NSArray *array = [self.navigationController viewControllers];
[self.navigationController popToViewController:[array objectAtIndex:2] animated:NO];
The first time user enters the app it goes: View1->View3 (The download screen no longer needed), and I have different push segue to go to View3 so lets assume the user goes to: View1->View 3->View4->View5, now he wishes to go back to View3, so the function:
NSArray *array = [self.navigationController viewControllers];
[self.navigationController popToViewController:[array objectAtIndex:2] animated:NO];
Will return him to View4, and this is WRONG. How can I solve it?
Well if you are using storyboards , what you can do is set your uiviewcontrollers' storyboard id and use it for popping and pushing your views.
Lets say your Storyboards name is MainStoryboard.storyboard
UIStoryboard* sb = [UIStoryboard storyboardWithName:#"MainStoryboard"
bundle:nil];
SettingsListViewController *settingsVC = [sb instantiateViewControllerWithIdentifier:#"SettingsListViewController"]; // #"SettingsListViewController" is the string you have set in above picture
[self.navigationController popToViewController:settingsVC animated:YES];
Above solution should work for you , but If I were you I would change the structure of my app , you say :
View1: First screen (Just logo)
View2: Download Screen
Since View1 is just logo and View 2 is also a view that you skip, you can remove them from navigation controller and make View3 your navigation controller's root view controller and when you need view1 and view2 present them as Modal View Controllers,
when you are done with them lets say; user successfully loaded the app dismiss logo screen and present Download Screen if download successful then dismiss it.
So your View3 Will be there as root view controller, lets say your at View(n) you want to go to home screen which is View3 all you need to do is call
[self.navigationController popToRootViewControllerAnimated:NO];
When you are on view(n) and want to pop to view(n-1) just use
[self.navigationController popViewControllerAnimated:YES];
good luck,
I always use this, and it will work fine in your case. In fact the following line of code is copied to my "Notes" for quick copy/paste.
Be sure to import your ViewController.h file, if not.
for (UIViewController *viewController in self.navigationController.viewControllers) {
if([viewController isKindOfClass:["your ViewController" class]]) {
[self.navigationController popToViewController:viewController animated:NO];
}
}
In the second sequence your navigation stack changes and view3 would be at index 1. so
[[self.navigationController popToViewController:[array objectAtIndex:1] animated:NO];
would be the right way of doing it.
according to your sitation use directly name of viewController
create instance of your viewController,like this
i supposed here that your viewController name is-view3Controller
View3Controller view3Controller=[[View3Controller alloc]init];
[self.navigationController popToViewController:#"view3Controller" animated:NO]
or if you are using storyboard then
View3Controller view3Controller=[self.storyboard instantiateViewControllerWithIdentifier:#"view3Controller"];
[self.navigationController popToViewController:#"view3Controller" animated:NO]
It sounds like View 2 is not being added to your view controllers array at run time, possibly because of the segue you've created.
Try removing the segue that transitions from View 1 > View 3 and pushing the user past View 2 without animating, as your application's logic requires it:
// If the user needs to skip ahead to view 3, conditionally push view 2 and view 3 without animating
[self.navigationController pushViewController:viewController2 animated:NO];
[self.navigationController pushViewController:viewController3 animated:NO];
Alternatively if you left the segue in place could you not look at the size of the UINavigationController viewControllers property and "guess" based on the size whether or not you skipped View 2? If you did then you can adjust the popToViewController method to pop to the correct index. This is, admittedly less elegant and brittle if you need to skip other views as well.
// Check length of viewController array with 'N' views (pseudo code)
if (self.navigationController.viewControllers.length == N-1)
// View 2 was ignored: pop to objectAtIndex:1
else
// View 2 was included: pop to objectAtIndex:2
If I understood you correctly, your view3 has special view controller, so you can use such code:
NSArray *VCs = [self.navigationController viewControllers];
for (UIViewController *VC in VCs)
{
if ([VC isKindOfClass:[**YOUR-VIEW-CONTROLLER** class]]) {
[self.navigationController popToViewController:VC animated:NO];
}
}
It's simple and it works!
for (UIViewController* controller in self.navigationController.viewControllers) {
if ([controller isKindOfClass:[<Your View Controller Name> class]]) {
[self.navigationController popToViewController:controller animated:YES];
return;
}
}
Go like this to a particular view controller
[self.navigationController popToViewController:[[self.navigationController viewControllers]objectAtIndex:1] animated:YES];
I'm trying to get working a simple operation. At least it seems simple. Ok, what I'd like to do is to push a view (with push view controller) from a view that has been pushed with modal view controller.
View1 --(push using modal view controller)-->View2--(push using push view controller)--View3.
Rigth now, i'm doing tests so i'm using a button to start the action. Here's the code I use to push from View2 to view 3:
//view2.h
UIToolbar *bar;
UIBarButtonItem *button;
UIToolbar *toolbar;
}
- (IBAction)demissModal:(id)sender;
- (IBAction)goView3:(id)sender;
#end
//view2.m
- (IBAction)goView3:(id)sender{
View3 *view_3 = [[View3 alloc] initWithNibName:#"View3" bundle:nil];
[self.navigationController pushViewController:view_3 animated:YES];
}
This is the same code I use to push View1 to View2, and it works. But when pushing View2 to View3, it's not working. Any idea of why happens that? Thanks!
View Controllers aren't actually 'modal' or 'push' view controllers. Modal or Push describe a transition between view controllers (called segues if you're using storyboards).
What I think you're asking is how to modally present a view controller, and then push another controller. The trick is when you modally present view controller #1, to actually present a navigation controller with its root view controller set as view controller #1.
MyViewController *myViewController = [MyViewController alloc] init];
UINavigationController *navController = [UINavigationController alloc] initWithRootViewController:myViewController];
// Presuming a view controller is asking for the modal transition in the first place.
[self presentViewController:navController animated:YES completion:nil];
// Now in myViewController, call [self.navigationController pushViewController:secondViewController animated:YES];
This is what it looks like using storyboards:
First of all, I'm not sure where that gegant_se is coming from.
Second of all, if you're pushing view2 from view1 the same way you're pushing view3 from view2, you're not using a modal.
Whenever you use a navigation controller to push a view controller, that view controller that was just pushed has a reference to the navigation controller, through the navigationController property. Try this:
[self.navigationController pushViewController:view_3 animated:YES];
Try this:
[self.navigationController pushViewController:view_3 animated:YES];
try this code AlarmList is view name .
AlarmListScreen *loscr=[[AlarmListScreen alloc]initWithNibName:nil bundle:nil];
[self.navigationController pushViewController:loscr animated:YES];
[loscr release];