[Fall in love with Swift] phase 13: Use Stroryboard page to jump and pass value

Source: Internet
Author: User


Most of the previous page jumps on the iOS on the Internet were in the form of the previous xib, but I use storyboard. This article only introduces the use of storyboard for page jump and pass value.

New page

The iOS program also uses the idea of MVC, the page file and the code file are separated, which is similar to that of Android. In the way of using storyboard, you only need to drag a View Controller into the storyboard to create a new page;

Next, you can add various controls to the newly created page to edit the newly created page.

Arranging various view controls on the newly created page is like editing the layout xml in Android, but to associate the program code with this view, some operations are required. First of all, it is necessary to create an associated class. At this time, you need to create an Objective-C class. The base class selects the corresponding View Controller base class. Here is the simplest UIViewController, With XIB for user Interface. Cannot check, click Finish to successfully create a View Controller.

Go back to the stroyboard view, select the view page you just created, and click the red frame

Select the corresponding View Controller in the red box, so that the view is associated with the program code file View Controller.

Page jump

After creating a new page, you can jump between pages. The simplest jump is to use a similar to creating a control outlet or binding event. Hold down the Ctrl key and drag to the page to jump to, and then pop up Window's selection modal:

Click the button after running the program to jump.

Another way to jump is to press the Ctrl key on the starting page and drag it to the target page. The options of the pop-up window are the same as above, then select the two page connection, and name the Segue in the red box :

Finally, add the following code in the place where the View Controller wants to trigger a jump (for example, click the button, then in the button click event method)

[self performSegueWithIdentifier: @ "segue ’s name" sender: nil];
Then you can jump.

To return to the previous page after jumping to a new page, add the following code where you need to return

[self dismissModalViewControllerAnimated: true];
The parameters true and false represent whether to use animation when switching the jumped page

Pass-by-page

The easiest way to pass values between pages is to define a global variable, whether it is jump or return, you can get / set to the value from the variable. If you do n’t use this method, you can use the prepareForSegue sender method to pass the value when jumping , You can use the protocol when you return. Let's take a look at them separately.

To use the prepareForSegue sender method to pass the value, you need to declare the attribute of the passed parameter in the View Controller class declaration of the target page, for example, that attribute is called value1, and then implement the method in the place of the start page

-(void) prepareForSegue: (UIStoryBoardSegue *) segue sender: (id)
{if ([segue.identifier compare: @ "mySegue"] == NO)
    {id page2 = segue.destinationViewController;
        [page2 setValue: self.lbUserName2.text forKey: @ "value1"];
    }
}
SetValue forKey is used to pass the value. The destinationViewController of segue can get the View Controller of the target page that the current Segue jumps to. There is a judgment here to determine whether the segue of this jump action is the one that needs to be passed by value, because if a page will have multiple Segue jumps to different pages, it is not so distinguished, in the View Controller of the target page If there is no corresponding parameter, an exception will be thrown.

The value is returned when using a protocol to return. The idea is this. The relevant protocol is defined. The protocol is implemented for the start page. The purpose is to open some methods for assignment to the target page. Those methods to return the return value to the start page, how to get the instance of the start page on the target page, then pass the value through the setValue forKey method on the page, the following gives an example

Define protocol

@protocol HGReturnView1Delegate <nsobject>
 
-(void) setReturnText: (NSString *) value;
 
@end </ nsobject>
The start page needs to implement the protocol, and the code for the implementation part will not be posted. The declaration of the target page needs to define one of the above protocol attributes:

@property (weak, nonatomic) id delegate;
The following code is called when the value is passed when the page jumps:

[self setValue: self forKey: @ ”delegate”];
When returning, you need to use the delegate property to force the conversion. After conversion, call the setReturnText method

NSObject <hgreturnview1delegate> * tmpDele = self.delegate;
[tmpDele setReturnText: self.txtReturn.text]; </ hgreturnview1delegate>
Here you can complete the value transfer between pages.

[Falling in Swift] 13th issue: Use StroryBoard page to jump and pass value

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.