Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to move to multiple view controllers?

I want to programmatically move from a viewController to another using Apple's new Swift language. I've googled and read through the docs, and I see how to use a single ViewController. Does anyone have an example or documentation on how to switch between View Controllers?

like image 852
slooker Avatar asked Jun 03 '14 19:06

slooker


People also ask

How do I switch between view controllers?

Right-click the control or object in your current view controller. Drag the cursor to the view controller you want to present. Select the kind of segue you want from the list that Xcode provides.

How do I segue to another view controller?

Well, in addition to that, Ctrl+dragging also helps set up segues. So, Ctrl+Drag from the “Go to Other View Controller” button, to somewhere in the second View Controller. It can be anywhere in the main box of the second view controller. When you release, it will show you a box like the one below.

How many view controllers should I have?

I know that many developers have a single View Controller for every screenful of content, but actually the answer is 8. This is one of the things you can do to make your app more composable and decoupled: use child view controllers. It's a very simple, but powerful technique.


1 Answers

There are many ways to do this detailed in the class documentation for UIViewController.

Example 1 Here is an example of using the -presentViewConroller: method (assuming this code is written in a UIViewController subclass):

var secondViewController = UIViewController() //create your second view controller. 
self.presentViewController(secondViewController, true, NULL)

Example 2 This is how you would present the second UIViewController via storyboards (again assuming this code is written in a UIViewController subclass):

self.preformSegueWithIdentifier("segueIdentfier", self);

like image 107
67cherries Avatar answered Sep 26 '22 13:09

67cherries