IOS development-use Storyboard for page Jump and value passing
Apple officially recommends that we build all the UIS using Storyboard, which is also a mature tool. Using Storyboard to build all interfaces, we can quickly build complex interfaces, which can save us a lot of time. We can also intuitively see the relationship between interfaces and make it easy to modify. In the future, if we need to make changes, we only need to find the corresponding Storyboard, which is much faster than before.
I will attach a Demo at the end of this article to help you have a more intuitive understanding. If you need it, you can download and run it.
In addition, we recommend that you first clear the content on the interface when trying every method to avoid errors. This is because it is easy for beginners to make the interface residual, which is not described here. Next, let's start our Storyboard journey!
Directory:
I. Use Storyboard to redirect
1) operations on the pure Storybard Interface
2) use code to jump
Ii. Pass the value on the Storyboard Interface
1) Use the prepareForSegue method to redirect:
2) Jump to the Storyboard Id
1. Use Storyboard to redirect 1) operations on the pure Storyboard Interface
We place a button on the Storyboard, click on the top of the button, and hold the "right-click" button, and drag it to the next page.
After you right-click it, a black and transparent pop-up box is displayed. Select the Redirect method:
We can see that there is a line connection between the interface and the interface, for example:
We can see in the toolbar on the Right of Xcode that we just connected (now we can run the project ):
2) use code to jump
We deleted the connection, as shown in:
Create a class CodeViewController that inherits from UIViewController:
Select the interface where the button is located and set its class files. By default, the class files are inherited from UIViewController, as shown in:
We changed it to the newly created class "CodeViewController", as shown in:
Scale down the interface (you can double-click the blank area or right-click to select the scaling ratio). This time, we do not directly use the "button" to connect the interface, but connect the interface to the interface, as shown in:
Note: The 100% zooming cannot be connected between the interface and the interface!
The subsequent operations are the same as the previous ones. To make it easy to understand, I will post one:
Select "this line" and specify an Identifier in the Identifier of Storyboard Segue. We will use it later:
In this case, we need to add an event to the button. Therefore, we need to display both the Storyboard and. m files as follows:
The operations for creating events are the same as those for connecting events:
Create a name for this event and click Connect:
Add the following code to the event to pass the newly connected Identifier. the sender is generally self:
[self performSegueWithIdentifier:@EasyCode sender:self];
In this way, the jump is successful.
Ii. Pass the value on the Storyboard interface 1) Use the prepareForSegue method to redirect:
We create two classes: PrepareViewController and ReceiveViewController.
Create two viewcontrollers on the Storyboard and connect the two interfaces. The operation steps are the same as those above (Code jump). This time, we define the Identifier of the link as "SendValue ":
Set the class on the interface to PrepareViewController, as shown in:
Select the second interface and set the inherited class to ReceiveViewController:
ReceiveViewController. h:
#import
@interface ReceiveViewController : UIViewController@property (strong, nonatomic) NSString *name;@property (assign, nonatomic) int age;@end
ReceiveViewController. m, of course, is output. Otherwise, how can I know whether the value is successfully transferred:
#import ReceiveViewController.h@interface ReceiveViewController ()@end@implementation ReceiveViewController- (void)viewDidLoad { [super viewDidLoad]; NSLog(@name is %@, age is %d, _name, _age);}@end
PrepareViewController. m file:
# Import PrepareViewController. h # import ReceiveViewController. h @ interface PrepareViewController () @ end @ implementation PrepareViewController-(IBAction) goAction :( id) sender {// jump to the target Vc [self defined mseguewithidentifier: @ SendValue sender: self];}-(void) prepareForSegue :( UIStoryboardSegue *) segue sender :( id) sender {// segue. identifier: Get the ID of the link if ([segue. identifier isEqualToString: @ SendValue]) {// segue. destinationViewController: Obtain the interface (VC) ReceiveViewController * receive = segue when connecting lines. destinationViewController; receive. name = @ Garvey; receive. age = 110; // you do not need to specify the jump here, because the jump code already exists in the button event. // [self. navigationController pushViewController: receive animated: YES] ;}}@ end
You can successfully receive the value (SUCCESS ):
2) Jump to the Storyboard Id
Drag two pure interfaces (VC) to the Storyboard. The first interface also has a UIButton:
IdViewController is the class I just created. The ReceiveViewController class is used on the receiving value interface, and the class inherited from the interface is (IdViewController and ReceiveViewController)
In the second interface, set the Storyboard id to "IdReceive", which will be used in subsequent jumps:
IdViewController. m
-(IBAction) action :( id) sender {// obtain the specified Storyboard. Fill in the Storyboard file name UIStoryboard * storyboard = [UIStoryboard storyboardWithName: @ Main bundle: nil]; // obtain the specified interface (VC) from the Storyboard according to identifier. identifier must be the only ReceiveViewController * receive = [storyboard instantiateViewControllerWithIdentifier: @ IdReceive]; receive. name = @ GC; receive. age = 10; [self. navigationController pushViewController: receive animated: YES];}
You can run the program and receive the value successfully!
Summary: Can you see the advantages and disadvantages of each method? It doesn't matter if you don't know it now. When you encounter real needs in the future, the advantages and disadvantages will be easily known. This is also a thing for you to think about.
Digress:
Some people do not agree to use Storyboard. Their points are as follows:
1) errors in Storyboard cannot be debugged, which often slows down development.
2) for multi-person development, version management is complicated.
3) Storyboard often has problems for no reason! They have a headache!
First, the Storyboard errors can be avoided. Second, Storyboard will not cause problems for no reason. If a problem occurs, it is often caused by the developer's operation. Why do I say this?
I used to encounter many errors caused by Storyboard. At that time, I often complained about Storyboard. But as I became more familiar with Storyboard, I found that all of them were previously caused by improper operations and program crash.
2nd: multi-person development, which can be divided into different modules. The modules under each person are different. It is not necessary for everyone to develop the same module. Of course, I don't know any special situations, so this should be determined based on the project size and complexity.
It took me one afternoon to write this article, so I felt tired and tired.
I hope this article will help you. Thank you.