Segueing to new Master and Detail controllers simultaneously in Swift - ios

I am creating a Split View application in Xcode. When an initial Master table view cell is selected I need to segue to a new detail and also replace the master with a new controller.
I almost achieved this by triggering the master segue from within prepareForSegue, this way it loaded both segues at the same time. This worked fine until in compact width where you see the app load one segue after the other. In compact size class I wouldn't need to load the detail.
How can this be achieved, is there a best practice?

In your prepareForSegue (in the place where you "load both segues at the same time"), you can check the state of your SplitViewController's collapsed property.
If it's not collapsed, you should perform the second segue, else you don't.

Related

UINavigationController for infinite navigation (nested folders)

I need to navigate inside folders and files in directory (from server). The problem is that I don't know the number of folders so it's not possible to use performSegueWithIdentifier statically. How can I use navigation controller with dynamically number of view controllers in swift? I want to "push" a new view controller every time a user tap on a folder in order to list files/folders inside it and I want to do it with UINavigationController so the user have the possibility to go back with "previous" button.
Both storyboard and programmatically approaches are ok.
Thanks you
Storyboards and segues are just a crutch. Think about how you would do this without them. At each level, to go down a level, you would just instantiate a new view controller and push it onto the navigation controller stack with pushViewController:animated:.
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UINavigationController_Class/#//apple_ref/occ/instm/UINavigationController/pushViewController:animated:
And in fact it takes only one view controller class to do this, since any instance can create and push another instance of its own class. The display of one folder is exactly like the display of any other.
So if you wanted to configure this notion in a storyboard, you would have a circular segue, that is, the view controller would have a push / show segue leading to itself.
I agree with #matt's answer, just create the controller and push it. For sake of completeness, you can do this in a Storyboard with a segue.
Here's how:
So that you can call the segue programmatically, add an additional prototype cell to your tableView. (You do this because you don't want the segue to be automatically triggered when the tableViewCell is selected. By using an additional prototype cell, the segue can be wired up, but it will never be triggered automatically since this prototype cell will never actually be instantiated.)
Control-drag from this prototype cell to the viewController icon at the top of the tableViewController. Select "Show" from the pop-up.
Find this segue in the Document Outline View and give it an identifier such as "showFolderSegue" in the Attributes Inspector.
Now, when you want to trigger the segue, call: self.performSegueWithIdentifier("showFolderSegue", sender: self)
You can use prepareForSegue to set up the new tableViewController as you normally would.
This method too works with a single tableViewController.

iOS 7 - Maintain view controller instance in Storyboard segue

I have an interactive custom view controller transition based on a storyboard segue (push).
The target view controller takes some time to be loaded as it contains a table with a lot of data; moreover when I leave this vc and come back, I need the table to maintain its content offset and not to start each time from the first row.
In order to achieve these two points I need the target vc to be a kind of singleton, and not to be deallocated/reallocated every time.
Any suggestion?
Thanks,
DAN
Don't use a segue -- they always instantiate new view controllers. Create a property for the destination view controller in the controller that initiates the transition, and only instantiate it the first time you go to it. Push the new controller in code.

Using Storyboards Segues

I have a question about how I use segues with Storyboard in Xcode.
I have an application with 3 views, and I would load them with animation modal "Cross Dissolve".
Every time I load a new view without dismiss the current one, it still occupying memory?
I'm realizing that after changing multiple views my APP becomes slow.
If yes, how is the right way to change views in sequence?
When you go back from 3 to 1 you should use an unwind segue. That will cause 3 and 2 to be deallocated (if you're not keeping a strong pointer to them), and you will actually go back to the same instance of 1 (rather than creating a new one). In general, you should not go backwards using a segue other than an unwind segue.
Rather than specifying the "back to 1" segue in the storyboard, you may want to instantiate it in code using the method -initWithIdentifier:source:destination:. That'll permit you to specify the destination as your first view controller instead of creating a new view controller to transition to.
In fact, you should probably specify all the segues between these view controllers programmatically if you don't want to instantiate new copies with each switch.

UISplitViewController on iPad with Storyboards?

In Xcode, how can I create a simple iPad application that uses Storyboards such that the DetailView controller is swapped out for each entry? Most examples that I've seen use the iPhone or they simply change the values in the same detail view controller.
I want to create the segues in Interface Builder from a static TableView Controller (with say 3 rows) where each row will load a different game detail view controller, which I would drag out and design in IB. Currently, when I connect a view controller with a segue, it replaces the navigation part of the UISplitViewController. In other words, it's like I'm traversing a tree, and I need to tell IB that I'm at a root node and I should be changing the Detail View.
A good starting point for segues are Lectures 6 and 7 of Stanford's CS193p Fall 2011 class.
http://itunes.apple.com/us/itunes-u/ipad-iphone-application-development/id473757255
The instructor, Paul Hegarty, covers everything. However, he runs out of time before the end of class to answer this question. He does include the source with the final solution in the file: Psychologist with Dr Pill.zip.
http://www.stanford.edu/class/cs193p/cgi-bin/drupal/downloads-2011-fall
Basically, all that needs to be done for this question is to Ctrl-drag from each UITableCell to the respective game detail view controllers then select the Replace segue. There is one more step because the view controller will shrink because by default Xcode thinks that you want to replace the master controller. For each of the segues, select the connection line and in the Attributes inspector then change the Destination from "Master Split" to "Detail Split". At this point, you can test with the popover, without writing any code.
Apple has provided sample code of a more general solution to the problem of how to swap out different detail views based on what is selected in the master view. The Apple example code accomplishes this by introducing a custom implementation of the UISplitViewControllerDelegate protocol:
https://developer.apple.com/library/ios/samplecode/MultipleDetailViews/Introduction/Intro.html
Hard to describe without pictures but: have a navigation controller as the master. Then hang each detailview off this with a named segue that replaces.
Then you need a bit of code.
In you master viewcontroller inside didSelectRowAtIndexPath, you need a switch statement based on indexpath.row and in each row call detailview performSegueWithIdentifier:#"the row you want"

In an iOS 5 Storyboard with a Split View Controller, how do you push a new View Controller to the Master and Detail from the same control?

When I click a control, I want it to push a new view controller to both the Master Split and the Detail Split.
Unfortunately, in the Storyboard editor, when you control drag from an object and create a segue to a View Controller, it overwrites the original value of the Push Storyboard Segue field.
Is it possible to accomplish my goal in the storyboard file, or does it have to be done in code?
You'll probably have to override -[UIViewController prepareForSegue:sender:] and do the extra push yourself; storyboards are designed to manage the navigation flow for a single view controller at a time.
I think it would be much nicer and cleaner if you would just Control+drag, but don't leave the segue with a default, "Push" style. Notice that there is also "Replace" style available for segues here. When you choose "Replace" style, there will be "Destination" parameter available. Choose whether you want to replace a view in Master, or Details controller, and that's it.

Resources