Preserve state of View Controller when using segues - ios

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.

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.

Perform modal segue in container view controller

I need to perform a modal segue in a container view controller. The structure is similar to the UISplitViewController. I want to modally present a view controller only above the left table view.
I've tried to set the context of the segue to "Current Context", but nothing changed. How can I perform the segue not in fullscreen, but only in the embedded controller?

Why doesn't my view controller have a back button?

In the picture below (We'll call them storyboard item A, B, C in order from left to right), View controller 'C' does not show a back button but I embedded it in a Navigation Controller. Other Stack overflow posts have not worked. How do I get the back button to show?
Move the Navigation Controller to the "first" view controller (ie: the view controller that does the "pushing" or "showing"). The Navigation Controller should not be embedded in the view controller that contains the back button but rather in the view controller upstream of it. This is the main cause of confusion.

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).

Navigating across multiple view controllers

I'm fairly new to iOS development. I'm working on an App that segues to multiple table view controllers. When my application launches, it loads view controller A. I then click on a row in View Controller A to segue to view controller B. The navigation button (back button) in View Controller B shows the title of View Controller A. I then click on a row in View Controller B which will segue to View Controller C. When I click a row in View Controller C, I segue back to View Controller B. My problem is that when I segue from View Controller C to View Controller B, the navigation button (back button) on View Controller B shows the title of View Controller C. I would like it to still show the title of View Controller A. Can anyone help on how this can be done? Sample code will be great. Thanks.
If you are going to use a segue to go from C to B, then you need to use an unwind segue. This has the advantage over using popViewControllerAnimated: in that you can use prepareForSegue to send information back to view controller B just like you would for a forward segue.
You can try to use the popViewControllerAnimated function. Have a look here : http://developer.apple.com/library/ios/documentation/uikit/reference/UINavigationController_Class/Reference/Reference.html#//apple_ref/occ/instm/UINavigationController/popViewControllerAnimated:
I think its pretty simple to use so I hope you will not need sample code.

Resources