IOS development-use Storyboard for page Jump and value passing
Topic StoryBoard
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.
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 Based on the ID of the specified line [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.