Use Swift to switch between different view controllers

Source: Internet
Author: User

It was easy to switch between pages when developing with Objective-c. In fact, there is no big change with Swift, if you are using storyboard to complete the interface, basically the same way, but in the code section written in the swift style of the line.

Today in the experiment to develop a simple small program, but encountered a number of bugs, and then to help StackOverflow on the big God solved the problem, in this record.

My program structure is this, in a page A has a button, and then click the button, then switch to another page B. Both A and B are in the same storyboard.

Here's a general approach:

    • Hand-coded view controller, which is not built in storyboard:

var VC = Viewcontroller () Self.presentviewcontroller (VC, Animated:true, Completion:nil) return

    • The following code can be used to create the storyboard:

Let SB = Uistoryboard (name: "Main", bundle:nil) Let VC = Sb.instantiateviewcontrollerwithidentifier ("Tabbarcontroller") As Viewcontroller Self.presentviewcontroller (VC, Animated:true, Completion:nil)

The Tabbarcontroller here is the name you opened in storyboard for the corresponding Viewcontroller, and then the Inspector ID of the identifier storyboard.

So my program is, in the Class A, define the following button action:

@IBAction func Login (Sender:uibutton) {let        sb = Uistoryboard (name: "Main", Bundle:nil) let        VC = Sb.instantiatev Iewcontrollerwithidentifier ("Tabbarcontroller") as Uitabbarcontroller        Self.presentviewcontroller (VC, animated : true, Completion:nil)    }
Note that I have not written viewcontroller,bug here as a post. When I first wrote the Viewcontroller, there was always a bug that prompted this:




I google about the dynamic Cast Class unconditional also did not find much useful information, there is no way only to help stackoverflow the great God, and soon someone reply:



It was because I read from storyboard that a control named Tabbarcontroller cannot be cast (as) into Viewcontroller because it is actually a uitabbarcontroller, that is, As after you want to force the conversion into a must be consistent with the storyboard.

So, the code is just a few lines, but not mechanically.

If your storyboard is Viewcontroller, as a viewcontroller, if Uitabbarcontroller as the Uitabbarcontroller, If it's something else like Uitableviewcontroller, you know what to do.





Use Swift to switch between different view controllers

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.