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