SoFunction
Updated on 2025-04-12

Share a storyboard jump and pass value

Knowledge written in the previous article:

Because Apple promotes Storyboard, and at present, Apple Watch also uses Storyboard to know that tomorrow's applications will probably be the world of Storyboard.
(The level is limited, and it is inevitable that something is wrong, I hope you can forgive it)

Many people seem to be using XIB, and they seem to be confused about how to jump Storyboard...

Well, given that in the morning, someone asked questions such as how to jump, how to transfer values, etc., I will give you some summary and provide you with some methods and references.
 ***

1. The easiest way

Drag and drop, there is no need to explain this. Just drag and drop directly to another view controller and select show.

2. Use the Segue method (here is mainly the value of method 1)

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

Then call the performSegueWithIdentifier method.

(Note: In the Demo, the TableViewController and SecondViewController are directly connected, instead of clicking on the Cell indicator to connect)

Perform the following method and you can perform the jump operation.

performSegueWithIdentifier("SecondSegue", sender: self)

How to pass values?

It's very simple, you need to call prepareForSegue method (because this is the parent view -> child view to pass the value, so you need to use destinationViewController)

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

(Note: Here, Swift's automatic completion may fail, so if the destinationViewController does not appear, it doesn't matter to hit it later.)

The text here is a variable I declared in the subview, which is used to set the value of Label.

PS:

Generally, we all use the method of using wires. Here I will tell you another way, which is to use viewWithTag. I set it to 100 in the Label control tag.

Of course, you can also use the connection. Use viewWithTag to be convenient for us to customize the cell without creating a separate Cell class.

3. Utilization

Using the method, there is no need to connect, and you can also jump between views. However, you must set the Storyboard ID.

Then use the following method to jump and pass values

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

Because it is jumping in the same Storyboard, the needs can be met.

Don't understand? Look at the last method:

4. Utilize UIStoryboard

For XIB method, we need to use nibName, and what should we do if we want to separate multiple sequences in different Storyboards?

At this time, it cannot be used.

But:

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

Does it feel like XIB? This way you can divide the Storyboard into several, and put a few Sences in each.

The advantage of this is that when you need to apply multiple modules with different functions, separate them into different Storyboards, they will not affect each other.

Compared with XIB, each folder only needs 1 Storyboard file and Swfit file.

The above description is about the relevant knowledge of storyboard jump value transmission, I hope you like it.