UINavigationController: Duplicate controller at position in stack - objective-c

I have a UINavigationController that gets a few views pushed onto the stack. Once I am a few levels in, I need to call up a modal view that is a copy of the UINavigationController and is at the same level in as the calling navigation controller.
Is this possible?

You should not interact with the tableView directly if it is not currently visible. (i.e. when another ViewController is on top of the stack)
A preferable solution to the problem you described in the comments is to modify the datasource and reload the tableView as soon as it is shown.

Bettr to add an button at the leftnavigationitem in each view and write action for popto RootView to remove the stack
[self.navigationController popToRootViewControllerAnimated:YES];
add back button in viewdidload
//To set the back buttin on Navigation bar
UIBarButtonItem *backButton = [[[UIBarButtonItem alloc] initWithTitle:#"<--" style:UIBarButtonItemStyleBordered target:self action:#selector(backclick:)] autorelease];
self.navigationItem.leftBarButtonItem = backButton
Now implwment action
- (IBAction)backclick:(id)sender
{
// To goback to the main view
[self.navigationController popToRootViewControllerAnimated:YES];
}

Yes, this is possible.
To push a new navigation stack modally, create a new navigation controller and populate its stack (setViewControllers:) with the views you want. You could use the same VC instances in this new stack, but I suggest instead new instances (perhaps of the same classes). The old stack will be hidden so long as this new, modal stack is in place. Dismiss the NavCon to get your old stack back.
If you just want to replace the VC in the current stack, you can pop the current VC (probably not animated!) and then push the new one. The user will be able to navigate through the existing stack using the left button in the nav bar, and navigate forward as you've implemented it.
Calling UINavigationController:setViewControllers replaces a navcon's entire stack with one call. This transition can be animated or not at your discretion.

Please allow me to summarize the answer with a little code snippet:
UIViewController *previousVC = nil;
if ([self.navigationController.viewControllers count] > 1)
previousVC =
[self.navigationController.viewControllers objectAtIndex:
([self.navigationController.viewControllers count] -2)];
else
previousVC = [self.navigationController.viewControllers objectAtIndex:0];

Related

What is the design pattern for navigating between ViewControllers?

I currently have 1 storyboard which contains 2 view controllers: ViewController and TableViewController. The ViewController is the login view, and the TableViewController is the page that displays results (results view).
Currently, I did not create a segue from the login view to the results view. Instead, on the login view, after a user presses the login button and is authenticated, I programmatically push to the results view as follows.
XYZResultsTableViewController* controller = [[XYZResultsTableViewController alloc]init];
UINavigationController *navController = self.navigationController;
[navController popViewControllerAnimated:NO];
[navController pushViewController:controller animated:YES];
Indeed, the results view shows, but there is a "< Back" button at the top left, which, if pressed, goes back to the login view.
So, my question are:
How do I get rid of the login view from the view stack? (so the back button on the results view does not show)
Is this "programmatic" way of navigating between views "bad"? Meaning, should I rely on the storyboard and segues instead? Should I navigate to a new storyboard (I've seen this question asked on SO, though I haven't visited it yet)?
I'm a newcomer, so any help is appreciated.
If you don't want to use the navigation stack, you have to use presentViewController instead of pushViewController
XYZResultsTableViewController* controller = [[XYZResultsTableViewController alloc]init];
[viewController1 presentViewController:controller animated:YES];//viewcontroller1 is current view controller
Never use the code below unless you want to have the navigationController stack in viewController you are showing
/*XYZResultsTableViewController* controller = [[XYZResultsTableViewController alloc]init];
UINavigationController *navController = self.navigationController;
[navController popViewControllerAnimated:NO];
[navController pushViewController:controller animated:YES]; */
for more information on this difference between presentViewController and UINavigationController?
http://developer.apple.com/library/ios/#documentation/uikit/reference/UINavigationController_Class/Reference/Reference.html

What is best approach to add back button for first view controller in navigation controller?

I'm beginner iOS developer, so don't know how to solve my problem correctly. I have one navigation controller and want to make modal transition to another navigation controller that will have back button on it's first view controller.
In my first navigation controller I made this:
InfoViewController *destinatinViewController = [[InfoViewController alloc] initWithNibName:#"InfoViewController" bundle:nil];
[self presentViewController:destinatinViewController animated:YES completion:nil];
InfoViewController is UINavigationViewController.
Then I made this in InfoViewController (second navigation controller):
InfoRootViewController *rootViewController = [[InfoRootViewController alloc] initWithNibName:#"InfoRootViewController" bundle:nil];
[self pushViewController:rootViewController animated:NO];
For now I know how to add button to go back, but it has not styling (certainly arrow). As I understand because it is only one view in stack of second navigation controller.
So, I need your help to add styled back button for first view controller of navigation controller, because I don't want to make own styling (I'm lazy).
Thanks, for any advance!
PS: I made modal transition because, it has animations like from bottom or top. I tried to use custom animations for pushViewController, but in iOS 7 they're working not correctly.
I'll let someone else help you with the back button issue because I think it's a mistake to create one for a modal view controller that's presented from the bottom of the screen. Typically, such a modal view controller is dismissed with a cancel button or its equivalent. If you really think that you need a back button then you should probably push the view controller onto the existing navigation stack instead of presenting it modally.
However, I am happy to show you how to properly alloc/init a navigation controller and then present it.
I don't know why you're subclassing UINavigationController, but I'm going to pretend that you don't need to (which is probably the case).
InfoRootViewController *rootViewController = [[InfoRootViewController alloc] initWithNibName:#"InfoRootViewController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
[self presentViewController:navController animated:YES completion:nil];

Cant pop view controller

I have 2 views, TableController and WirelessController. While in TableController I need to pop the WirelessController view. This is what I've tried and nothing happens, no console output either.
WirelessController *wCon = [[WirelessController alloc] init];
[[wCon navigationController] popViewControllerAnimated:YES];
and this has the same problem.
[self navigationController] popViewControllerAnimated:YES];
Is it the fact that I'm using the UINavigationController when it's a view based app?
I think you mean to push, not pop...
WirelessController *wCon = [[WirelessController alloc] init];
[[self navigationController] pushViewControllerAnimated:YES];
Push adds a new item to the top of the stack; pop removes the top item from the stack.
Update
From your comments it seems that...
your first view is an instance of WirelessController.
from there you present a TableController modally
now you want to return to you wirelessController.
In this case you need to send a message back to the presenting view controller (wirelessController) asking it to dismiss the view controller it has presented (tableController)
In tableController:
[self presentingViewController] dismissViewControllerAnimated:YES
completion:nil]];
Whatever is going on, you certainly don't want to do this:
WirelessController *wCon = [[WirelessController alloc] init];
This line will create a new object. You want to return to an existing object.
Pushing and popping viewControllers is an activity generally associated with navigation controllers, which keep an array of managed viewControllers. In that case you would push to add a new controller to the top of the stack, and pop to remove it from the stack. In the absence of a navigation controller, there is no such stack, so push and pop make no sense.

Hide FBFriendPickerViewController navbar when pushing onto UINavigationController

Presenting an instance of FBFriendPickerViewController using presentViewController:animated:completion: is pretty straightforward and the class seems like it is meant for that use case. However, I want to push an instance of FBFriendPickerViewController onto an instance of UINavigationController using pushViewController:animated:.
Consider the following code as an example:
self.fbFriendPickerController = [[FBFriendPickerViewController alloc] init];
self.fbFriendPickerController.hidesBottomBarWhenPushed = YES;
// configure stuff
[[self navigationController] pushViewController:self.fbFriendPickerController animated:YES];
However, the problem is that the instance of FBFriendPickerViewController already has a top navigation bar. When pushed onto a UINavigationController, this results in two top navigation bars stacked vertically, as you can see in the screenshot below.
One solution would be to hide the top nav bar of the UINavigationController, but that creates an awkward transition and there is no back button. Any thoughts on the best way to keep the UINavigationController top nav bar but the hide the FBFriendPickerViewController top nav bar?
After looking through the Facebook iOS SDK source code on Github, I figured this out. FBFriendPickerViewController is a subclass of FBViewController. If you set the doneButton and cancelButton properties of any FBViewController to nil, FBViewController will remove the top navigation bar. As a result, the following code works:
self.fbFriendPickerController = [[FBFriendPickerViewController alloc] init];
self.fbFriendPickerController.hidesBottomBarWhenPushed = YES;
self.fbFriendPickerController.doneButton = nil;
self.fbFriendPickerController.cancelButton = nil;
// configure stuff
[[self navigationController] pushViewController:self.fbFriendPickerController animated:YES];

NavigationController Back Skip View

First off, I am aware of this question being asked in a forward manner, but in this case I am asking for a backwards manner in which the Navigation Controller is already designed. With that being said...
I have a UINavigationController with three views: Table, Get, and Avail in that order that was created in IB.
When going forward, I want to go from Table to Get to Avail, but when I hit the "Back" button on Avail I want to skip over Get and go directly back to Table. Is this possible? If so, how?
Here's how I did it:
NSArray *VCs = [self.navigationController viewControllers];
[self.navigationController popToViewController:[VCs objectAtIndex:([VCs count] - 2)] animated:YES];
To be able to override the nav controllers's back button you're going to have to subclass UINavigationController. Check out how in this tutorial: http://www.hanspinckaers.com/custom-action-on-back-button-uinavigationcontroller
Implement the navigation controller's delegate method:
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
It will fire for all VCs in the nav controller stack. (put this code in the VC immediately after your navigation controller, and put < UINavigationControllerDelegate > in the .h)
What you want to do is replace the entire Navigation Controller's stack without the view controller you want to remove in it. Since it's a non-mutable array, you have to convert it to a mutable array before removing the VC, remove the VC, then replace the navigation controller's stack with the call [self.navigationController setViewControllers:newVCs animated:NO];. That's the crucial part. You are replacing the stack after you have loaded the page you are on but since you're keeping the VC you're on, it's still the top item on the stack so there is no visible impact to the user. As long as you don't have a lot of VCs on the stack, it's not an expensive call.
Here's how I did it in the delegate method:
//Remove list setup page if new list was created
if ([self.navigationController topViewController].class == [ItemViewController class])
{
NSArray *VCs = [self.navigationController viewControllers];
if(((UITableViewController*)[VCs objectAtIndex:[VCs count]-2]).class == [NewCardTypeController class])
{
NewCardTypeController *removedObject = [VCs objectAtIndex:[VCs count]-2];
if(removedObject != nil)
{
NSMutableArray *newArray = [NSMutableArray arrayWithArray:VCs];
[newArray removeObject:removedObject];
NSArray *newVCs = [NSArray arrayWithArray:newArray];
[self.navigationController setViewControllers:newVCs animated:NO];
}
}
}
Take a look at UINavigationController's -popToViewController:animated: and -popToRootViewControllerAnimated:, which do exactly what you're asking for. That is, they pop the navigation stack back to a particular view controller, or to the root view controller. You'll still need to intercept the nav controller's back button action to use them, though.