Push segue without animation - cocoa-touch

I'm writing a storyboard-based iPhone app and working on state restoration. When performing the segues normally, I want to have them animate, but when I'm restoring several levels of a navigation hierarchy, I only want the last segue to animate. Other than setting up two sets of segues—one set that uses a normal push segue, and another that uses a custom non-animating push segue—is there any way to achieve what I'm trying to do?

It's possible to directly manipulate the view controller stack, independently of the application's segues or storyboards.
You can use this technique to restore a deep stack of view controllers, and perform / animate just a single segue to the top view controller. (You will likely need to create a specific push segue for this purpose.)
For example, to restore a two view controller stack, you could do the following. In this example, it's assumed that some action on an existing view controller leads to the state restore, but you can just as easily perform it from your App Delegate.
[self performSegueWithIdentifier:#"Page2Express" sender:self];
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"Page2Express"])
{
// Get any state data you need to from Core Data
CoreDataType *valuePulledFromCoreData = // ...
// Set up the page 2 view controller as you normally would
Page2ViewController *page2ViewController = segue.destinationViewController;
page2ViewController.instanceVariable = valuePulledFromCoreData;
// Create a loose, page 1 view controller and set it up as required
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:nil];
Page1ViewController *page1ViewController = [storyboard instantiateViewControllerWithIdentifier:#"Page1ViewController"]; // Ensure you have this identifier set up in your storyboard
page1ViewController.instanceVariable = valuePulledFromCoreData;
// Add the page 1 view controller to the top of the navigation stack (to be later obscured in the segue by the page 2 view controller)
NSMutableArray *viewControllers = [[self navigationController].viewControllers mutableCopy];
[viewControllers addObject:page1ViewController];
[self navigationController].viewControllers = viewControllers;
}
}
If, instead, you prefer to have no animations, then it's easier still. You can restore state solely by manipulating the view controller stack (and without using any segues) from - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions and - (void)applicationWillEnterForeground:(UIApplication *)application.
Either way will work seamlessly and in tandem with your existing storyboard(s) and segues.

Related

iOS 8 UISplitViewController delegate not responding

I'm transitioning my app to iOS 8, and I've decided to use a SplitViewController because its new functionality finally allows me to do what I want. I present the SVC modally on iPad, and the SVC is the root view controller of the full-screen cover vertical transition. From the presenting view controller:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
plotSplitViewController = segue.destinationViewController;
plotViewController = (PlotViewController *)[[[segue.destinationViewController viewControllers] objectAtIndex:1] topViewController];
plotViewController.inventory = _inventory;
if ([plotViewController view]) [plotViewController setPlot:selectedPlot];
}
Then I manually make connections in the PlotSplitViewController:
- (void)viewDidLoad
{
[super viewDidLoad];
self.delegate = self;
// set up controllers
layoutNavigationController = [self.viewControllers objectAtIndex:1];
plotViewController = (PlotViewController *)[layoutNavigationController topViewController];
plotViewController.delegate = self;
// configure split view
[self showInfoPane:NO withTable:infoTableViewController];
self.preferredDisplayMode = UISplitViewControllerDisplayModePrimaryHidden;
}
So both the master and the detail get view controllers inside UINavigationControllers (to take advantage of the free toolbar resizing, plus the master pushes a table view hierarchy).
Everything seems fine; the views load as they're supposed to, the delegate method from PlotViewController functions correctly, etc. But as you can see, I assign the Split View Controller to be its own delegate...but it won't respond to any of its own methods, so I can't customize its behavior. I checked to make sure it's set correctly:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
NSLog(#"split view %# did appear, delegate: %#", self, self.delegate);
}
And it returns the same object (itself) for both values. Is this just a no-no? I read that you can assign a SplitViewController as its own delegate, and I think an object can be a delegate for more than one other object, right? It can certainly implement protocols for more than one. So why is my SplitViewController not able to receive delegate methods for itself? I have NSLogs in all of them, and none are ever called.
Turns out it works if you make your custom view controller a subclass of UIViewController, and add a UISplitViewController programmatically, as a child view controller. Make your VC its delegate, and you can make it behave however you want.

how to display various view controllers (each having its respective navigation controller) using storyboard ID

I'm working on my first app. Here's what I want to accomplish:
There will be a menu with several different options. For simplicity, assume this is comprised of UIButtons with IBAction outlets and the functionality exists to pull up the menu at any time.
Each menu button, when pressed, should display a different navigation controller's content. If the user brings up the menu and makes a different selection, the navigation controller in which he is currently operating should not be affected; the newly selected navigation chain is displayed on top of the old, and through the menu, the user can go back to the view where he left off on the previous navigation chain at any time.
visual illustration (click for higher resolution):
Please note that there are 3 different navigation controllers/chains. The root view controller (which is also the menu in this simplified version) is not part of any of them. It will not suffice to instantiate one of the navigation chains anew when it has been previously instantiated, and here's why: if the user was on screen 3 of option 2 and then selects option 1 from the menu and then selects option 2 (again) from the menu, he should be looking at screen 3 of option 2--right where he left off; the view controller he was viewing when he previously left the navigation chain should be brought back to the top.
I can make a button instantiate and present a view controller from the storyboard if there is NOT a navigation controller:
- (IBAction)buttonPressed:(id)sender {
UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:#"View 2"];
[self presentViewController:controller animated:YES completion:nil];
}
However, I can't figure out how to make those two methods work with a navigation controller involved. Moreover, I'm not sure those two methods are the right choice, because I won't always want to instantiate a new view controller: when a menu button is pressed, a check should be performed to see if the view (navigation?) controller with the corresponding identifier has already been instantiated. If so, it should simply be made the top view controller.
In summary, here are my questions:
1) How should I instantiate and display a view controller that is embedded in a navigation controller, preferably using a storyboard ID? Do you use the storyboard ID of the navigation controller or of the view controller?
2) How should I check whether an instance already exists? Again, should I check for an extant navigation controller or for a view controller, and what's the best method to do so?
3) If the selected navigation chain has already been instantiated and is in the stack of view controllers somewhere, what is the best method for bringing it to the top?
Thank you!!
side note -- it would be nice to know how to paste code snippets with indentation and color formatting preserved :)
As Rob has suggested, a tab bar controller would make a good organising principle for your design.
Add a UITabBarController to your storyboard, give it a storyboard iD. Assign each of your three sets of viewControllers ( with their respective navController) to a tab item in the tabBarController.
UITabBarController
|--> UINavigationController --> VC1 ---> VC2 -->
|--> UINavigationController --> VC1 ---> VC2 -->
|--> UINavigationController --> VC1 ---> VC2 -->
In you app delegate make a strong property to hold your tab bar controller's pointer. As the tab bar controller keeps pointers to all of it's tab items, this will take care of state for each of your sets of viewControllers. You won't have to keep separate pointers for any of them, and you can get references to them via the tabBarController's viewControllers property.
#property (strong, nonatomic) UITabBarController* tabVC;
Initialise it on startup
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIStoryboard storyBoard =
[UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:nil];
self.tabVC = [storyBoard instantiateViewControllerWithIdentifier:#"tabVC"];
//hide the tab bar
for (UINavigationController* navController in self.tabVC.viewControllers)
[navController.viewControllers[0] setHidesBottomBarWhenPushed:YES];
return YES;
}
An alternative way to hide the tab bar is to check the "Hides bottom bar on push" box in the Attributes Inspector for each of the (initial) viewControllers. You don't have to do this for subsequent viewControllers, just the first one that will be seen in that tab item.
Then when you need to navigate to one of your navController groups…
- (IBAction)openTab:(UIButton*)sender {
AppDelegate* appDelegate =
(AppDelegate*)[[UIApplication sharedApplication] delegate];
if ([sender.titleLabel.text isEqualToString: #"Option 1"]) {
appDelegate.tabVC.selectedIndex = 0;
}else if ([sender.titleLabel.text isEqualToString: #"Option 2"]){
appDelegate.tabVC.selectedIndex = 1;
}else if ([sender.titleLabel.text isEqualToString: #"Option 3"]){
appDelegate.tabVC.selectedIndex = 2;
}
[self presentViewController:appDelegate.tabVC
animated:YES completion:nil];
}
(this example uses presentViewController, your app design may navigate this in other ways…)
update
If you want to do this without a tab bar controller, you can instantiate an array holding pointers to each of your nav controllers instead:
UINavigationController* ncA =
[storyboard instantiateViewControllerWithIdentifier:#"NCA"];
UINavigationController* ncB =
[storyboard instantiateViewControllerWithIdentifier:#"NCB"];
UINavigationController* ncC =
[storyboard instantiateViewControllerWithIdentifier:#"NCC"];
self.ncArray = #[ncA,ncB,ncC];
Which has the benefit of not having a tab bar to hide…
Then your selection looks like…
- (IBAction)openNav:(UIButton*)sender {
AppDelegate* appDelegate =
(AppDelegate*)[[UIApplication sharedApplication] delegate];
int idx = 0;
if ([sender.titleLabel.text isEqualToString: #"option 1"]) {
idx = 0;
}else if ([sender.titleLabel.text isEqualToString: #"option 2"]){
idx = 1;
}else if ([sender.titleLabel.text isEqualToString: #"option 3"]){
idx = 2;
}
[self presentViewController:appDelegate.ncArray[idx]
animated:YES completion:nil];
}
1 / You can instantiate a viewController in your viewDidLoad method of your main viewController, so it will be instantiate 1 time only.
Now if you want display your controller, you would better push it :
- (IBAction)buttonPressed:(id)sender {
// Declare your controller in your .h file and do :
controller = [self.storyboard instantiateViewControllerWithIdentifier:#"View 2"];
// Note you can move this line in the viewDidLoad method to be called only 1 time
// Then do not use :
// [self presentViewController:controller animated:YES completion:nil];
// Better to use :
[self.navigationController pushViewController:controller animated:YES];
}
2 / I'm not sure, but if you want to check if an instance already exist just check :
if (controller) {
// Some stuff here
} // I think this checks if controller is initiated.
3 / I know it's not a good advice but I would tell you to not worry about checking if your controller already exist, because I think it's easier to access your viewController by using the 2 lines again :
controller = [self.storyboard instantiateViewControllerWithIdentifier:#"View 2"];
[self.navigationController pushViewController:controller animated:YES];
4 / I'm not sure if colors can be used here because of a specific style sheets.
I'm not sure to really have the good answer to your question but I hope this will help you.

Change Storyboard Nav View Controller on app open without noticeable transition.

I have a basic ui navigation view with a table view, when a user selects a name and presses the top bar next button it should go to the main ui navigation view, after this initial setup it should start on the main view controller what's the best way to go about this?....
I currently have this:
My end goal is on complete of the LoginNavController it would "push" to the new NavController and on open always go straight too the new NavController. How do I do this in a efficient (and proper?) way?
I'm not sure what you mean with
My end goal is on complete of the LoginNavController it would "push" to the new NavController and on open always go straight too the new NavController. How do I do this in a efficient (and proper?) way?
If what you mean is that you would like to show the login view only once, and then always show the other view controller, one possible solution would be to use a modal segue to the next navigation controller (identified by the ID modalSegue in my code snippet). Once the login view controller has achieved its goal, you could save it through the use of NSUserDefaults (these are persistent between app launches). In the viewWillAppear method of the login view controller you could then check the value for the relative key, and if it is set then perform directly the segue to the other navigation view controller. It would be something like:
-(void)viewWillAppear:(BOOL)animated
{
NSNumber* result = [[NSUserDefaults standardUserDefaults] valueForKey:#"isSet"];
if(result.boolValue)
[self performSegueWithIdentifier:#"modalSegue" sender:self];
}
And to set the key once the loginViewController is done you would do something like:
-(void) setLoginCompleted
{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"isSet"];
}
EDIT: For a smoother user experience, you could directly set the rootViewController of your application window in the didFinishLaunchingWithOptions: delegate's method. In this case you would do something like:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
MyNavController *nav = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:#"myNavVC"];
NSNumber* result = [[NSUserDefaults standardUserDefaults] valueForKey:#"isSet"];
if(result.boolValue)
[self.window setRootViewController:nav];
return YES;
}

How to presentModalViewController without dismiss the TabBarController

Hey guys i`m trying to present a modal view controller inside an application with a tab bar controller. The problem is, every time the new view is presented, it on top of my tab bar.
I need to keep my tab bar even when the view is presented. Like google maps application does with his toolbar at the bottom of the screen.
How can i do that?
Thank you
By default, a modal view controller is meant to take up the entire screen (on an iPhone/iPod, at least). Because of this, it covers whatever you have on screen at the time.
A view controller presented via modal segue is meant to live on its own. If you want to keep your Navigation and TabBar, then just use a push segue to present the new ViewController. Remember to use this kind of segue, your presenting controller needs to be part of a UINavigationController already.
Use this to push a ViewController. If it is a UINavigationController it will push its linked RootViewController by itsself.
Create a viewController to push: (Using Storyboard)
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"LoginViewController"];
or (Using Code/Nibs)
LoginViewController *viewController = [[LoginViewController alloc] init]; //initWithNibNamed in case you are using nibs.
//in case you want to start a new Navigation: UINavigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
and push with:
[self.navigationController pushViewController:vc animated:true];
Also, if you are using Storyboards for the segues you can use this to do all the stuff. Remember to set the segue identifier.
[self performSegueWithIdentifier:#"pushLoginViewController" sender:self]; //Segue needs to exist and to be linked with the performing controller. Only use this if you need to trigger the segue with coder rather than an interface object.
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"pushLiftDetail"]) {
[[segue.destinationViewController someMethod:]];
segue.destinationViewController.someProperty = x;
}
}
I think you'll need to add a UITabBar to the modal view and implement/duplicate the buttons and functionality that your main bar has. The essence of a modal window is it has total control until it is dismissed.
You might try putting your UITabBarController into a NavBarController, but I'm not certain that this will work.
UITabBarController -> NavBarController -> Modal View

How to replace RootViewController in "Navigation-based application"

I have an application that uses the "navigation-based application" template in XCode.
Now I want to change it so that the first view that loads is a regular (custom) UIView, and if the user clicks a particular button, I push the original RootViewController onto the NavigationController.
I understand that somewhere, someone is calling this with my RootViewController:
- (id)initWithRootViewController:(UIViewController *)rootViewController
I want to know how to replace the argument with my new class.
if you want to replace the root view controller of your navigation stack you can replace the first object of its view controllers array as -
NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:[self.navigationController viewControllers]];
NewViewController *nvc = [[NewViewController alloc] init];
[viewControllers replaceObjectAtIndex:0 withObject:nvc];
[self.navigationController setViewControllers:viewControllers];
^ These are all ways to do it programmatically. Thats cool. But I use the interface builder and storyboards in Xcode, so this is the easy and fast way to add a new view controller:
Open the storyboard in question
Add a new view controller to your storyboard by dragging it from the objects list (right hand tool bar bottom)
While holding down the CONTROL key, click and drag from the middle of your navigation controller (should be blank and gray) to your new fresh white view.
On the popup, selection Relation Segue: Root View Controller (should be below the normal push/modal/custom options you have likely seen before)
Tada! Enjoy your new root view controller without holding your day up with programmatic creation.
Look inside the main app delegate .m file and find the method
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
Inside it will be a line like this
self.window.rootViewController = self.navigationController;
You can instantiate a diffent view controller there and assign it to be the rootViewController