Passing message from root view controller to another view controller - ios

I have a navigation root view controller, which I am pushing to another view controller.
The view controller will determine the third pushed controller based on what button was pressed in root view controller. Based on this, how can I send a message through delegate from the root view controller to the second view controller 2 that either button 1 or button 2 has been pressed?

If the difference is a change in state for the application, either create a property on your application delegate, or create a manager class to handle your application state. Set the property / notify the manager when you touch the button in your root view controller, then check this value when your third view controller loads.
If the difference is merely that are navigating to a different area of the app, create a property on your second view controller and your third view controller. When you touch the button in your root view controller, set the property on the second view controller. When you navigate from the second view controller to the third view controller, set the property on the third view controller before you push it onto the stack.

Related

Push and present view controllers with a single push animation

From my navigation controller's root view controller Root
I want to push a view controller A
which then instantaneously presents another view controller B.
How can I do both at the same time with a single push animation?
(💡 The idea behind this is that view controller A allows for editing some content. If no content has been created, it needs to show view controller B first which allows the user to enter a title and then create the content.)
What I've tried:
When I do the following in view controller A's viewDidLoad() method:
if content == nil {
let createContentViewController = // instantiate new view controller instance
present(createContentViewController, animated: false)
}
UIKit also omits the push animation when animated is set to false – so I get no animation at all. When animated is set to true, I get a double animation (first push, then modal). 🙄
The reason you're having trouble doing what you describe is that you can't present View Controller B on View Controller A until View Controller A is part of the view controller hierarchy — and until you have pushed it, it isn't.
I would suggest, therefore, not using a presented view controller in this story at all. All you are really describing, it seems to me, is a View Controller A and its main view (View A) with a View B in front of it. That's something you can readily prepare between creating View Controller A and pushing it. View B can still contain a Dismiss button or similar to which you respond by sliding it off the screen, revealing View A.
If you really need a View Controller B for purposes of code organization, then have View Controller A be a custom parent view controller for View Controller B. A cool feature of that solution is that the whole thing can be configured in the storyboard using an embed segue. If we push View Controller A and you don't need View Controller B, you just hide View B before pushing.

Preserve state of View Controller when using segues

I have a View Controller (A) with a text field and some other things in it.
When the user presses a button on View Controller A it segues to View Controller B using "Present Modally".
How could I preserve the state of View Controller A (e.g. text in the textfield) when returning to it from View Controller B. I would rather avoid using NSUserDefaults if possible.
Thanks!
When you present view controller B modally on top of view view controller A, view controller A is not closed - it's just covered by view controller B. The close action on view controller B should invoke dismiss(animated:completion:) to dismiss the modal. When you do that you can be certain that view controller A will be revealed with its state intact.
You should NOT use a segue to go back to view controller A. That would create a new copy of view controller A that would wind up being displayed on top of the original view controller A and the new view controller B. That is a bad idea.

In iOS is the Initial View Controller always the same as the Root View Controller

In the 'Start Developing iOS Apps Today' guide by Apple it says that, "the first item added to the [navigation] stack is the Root View Controller and is never popped off the stack." Later on in the same section it goes on to say, "one of the view controllers is marked as the Initial View Controller ... this is the view controller that will be displayed the first time the app is launched."
My question is are the Initial View Controller and the Root View Controller always the same thing or can they be different? For example, if you created a game where the Root View Controller was the view where you played the game could you have a different controller (maybe the start screen) be the Initial View Controller, and how would this work?
For example, if you created a game where the Root View Controller was
the view where you played the game could you have a different
controller (maybe the start screen) be the Initial View Controller,
and how would this work?
Let's say for the sake of argument that the game uses a navigation controller to manage its various view controllers. In that case, the nav controller would likely be the initial view controller as well as the window's root view controller. The game board view controller might then be the nav controller's root view controller.
If you wanted to show a "game start" view controller at the beginning of the game, there are at least three reasonable options:
Make the game start view controller the nav controller's root and push the game board controller onto the nav stack when the user starts the game.
Present the game start view controller modally, and dismiss it when the user wants to start the game.
Make the game start view controller the initial view controller (and the window's root view controller), and then present the navigation controller (with the game board view controller as it's root) modally.
So no, the "initial" view controller doesn't need to be the view controller that the user actually sees first, it's just the one that's loaded first from the storyboard. It may contain other view controllers, or it might cause some other view controller to be presented immediately.
There are two root view controllers in play here:
Your application's key UIWindow's rootViewController. (Most apps only have one UIWindow but some have more than one.)
A UINavigationController's root view controller (the first object in its viewControllers array).
The Initial View Controller in a storyboard will typically be set as your key window's root view controller (#1), although this too has exceptions.
If that happens to be a navigation controller (this is common), then that navigation controller will have its own root view controller (#2).
The initial view controller is associated with the storyboard which does the work for you in terms of making that the root view controller of the window.
You can have further root view controllers inside your application but these are secondary and separate to the window's root view controller.

Showing a UISplitViewController inside a pop over

I'm wanting to create a UI where I have a popover that comes from a button that and contains a split view UI with two table view controllers side by side.
The storyboard I have now has a normal page with a button, the button has a popover segue to a split view controller.
The split view controller has a master relationship to a navigation controller which has a root view controller of a table view controller.
The split view controller has a detail view controller to another navigation controller which again has a root view controller of a table view controller.
When I launch the pop up it only ever displays the master controller, not the two side by side.
UISplitViewCpntroller can only be the root view of an app - as such, you cannot put them in a UIPopover or any other non-root view.
You would have to create your own UISplitViewCpntroller type view (or look for some open source code).

iPad - Page view controller - show next view controller

I'm using page view controller with a button on top right. on click of it it will show pop over view which has table view. (Table view has list of URL's)
On selecting a cell, i want to push a view controller which has web view that shows the URL in the cell.
Upto showing pop over its fine. I'm not able to find a solution on how to push another view controller.
Any solution for this?
I added a protocol in my table view controller class and implementing the delegate methods in Root View controller. When the cell is clicked I am calling appropriate delegate method which instantiates the next view controller and pushes it from navigation controller.

Resources