Share an Storyboard jump with pass value

  • 2020-05-13 03:29:35
  • OfStack

The knowledge written in front:

Because apple USES Storyboard and for now, Apple Watch also USES Storyboard, so it's going to be all Storyboard tomorrow.
(the level is limited, the wrong place is unavoidable, hope to forgive)

A lot of people still seem to be using XIB, and they seem to have no idea how to jump Storyboard...

Ok, in view of the morning group, some people asked how to jump, how to pass the value and so on. Let's make a summary, at the same time to provide you with some methods and reference.
***

1. The easiest way

Drag, I don't need to explain this, just drag to another view controller, select show, and that's it.

2. Use the Segue method (mainly method 1)

Connect the line, click the middle part of the line and set Identifier.

The performSegueWithIdentifier method is then called.

(note: in Demo, you connect TableViewController and SecondViewController directly, instead of clicking indicator of Cell.)

Perform the following method and you are ready to jump.


performSegueWithIdentifier("SecondSegue", sender: self)

How to pass values ?

Quite simply, you need to call the prepareForSegue method (because this is the superview - > Subviews pass values, so destinationViewController)


 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var theSegue = segue.destinationViewController as SecondViewController
theSegue.text = "Pass"
}

(note: here, the auto completion of Swift may fail, so if destinationViewController does not appear, it doesn't matter to call back.)

Here, text is the variable I declared in the subview to set the value of Label.

PS:

Generally, we all use the wired method, and here is another method, viewWithTag. I previously set Tag to 100 in Label control Tag.

Of course you can also use wires, viewWithTag can be used when we customize Cell without creating a separate Cell class.

3. Use self. storyboard

With the self.storyboard method, there is no need to connect the lines, and one sample can jump between views, but Storyboard ID must be set.

Then use the following method to jump and pass the value


 var thirdVC =  
 self.storyboard?.instantiateViewControllerWithIdentifier("ThirdViewController") 
 as ThirdViewController
thirdVC.text = "Pass"
self.navigationController?.pushViewController(thirdVC, animated: true) 

Since the jump is in the same Storyboard, self.storyboard can meet the requirements.

Don't understand the & # 63; Look at the last one:

4. Use UIStoryboard

XIB method, we need to use nibName, also what if we want to separate multiple sence in different Storyboard?

At this point, self cannot be used.storyboard cannot be used.

But:


var storyboard = UIStoryboard(name: "New", bundle: nil)
var newVC = storyboard.instantiateViewControllerWithIdentifier("NewViewController") as NewViewController
newVC.text = "Pass"
self.navigationController?.pushViewController(newVC, animated: true)

Is it similar to XIB? In this way, we can divide Storyboard into several parts and put several Sence into each part.

The advantage of this is that when you need to do multiple application modules with different functions, they are separated into different Storyboard without affecting each other.

Compared to XIB, only 1 Storyboard file and Swfit file are needed for each folder.

The above description is this site to share storyboard jump pass value of relevant knowledge, hope you like.


Related articles: