Note: When navigating between artboards in vs2015, use CTRL + the left mouse button to navigate the settings. Links to Artboards using Navigationcontroller .... Switching with Storyboard Segue implementation Uiviewcontroller (instance)
Blog Category:
- Phone/ios/objective-c/Swift
Storyboard is a term that appears in the IOS 5 SDK, which is actually the original Xib file (Interface Builder), used to make interface typesetting tools, and of course there are many more applications in Storyboard, of which Storyboar D Segue can make you almost no code to write, easy to complete two uiviewcontroller switch work, the following is our demonstration.
First, when opening a new project, select single View application to simplify the process, you can make a uiviewcontroller and its corresponding class file, if you need to open a completely blank clean new project, and then add yourself Storyboard and Uiviewcontroller.
Then to the project under the storyboard screen, from the bottom right of the library to pull a uiviewcontroller to storyboard, and the two Uiviewcontroller to do interface on the design: add Navigation Bar to distinguish Knowledge of each other, with one used to do page toggle buttons, such as.
We want to press the button on page 1 to page 2, the screen can be switched to page 2, after the same method can be returned to page 1, in order to achieve the purpose, must use the Storyboard Segue element will be two Uiviewcontroller, to connect up, due to Storyboar D Segue is generated dynamically, so it does not appear in the component library of the component Storyboard.
The way to produce Storyboard Segue is often simple, just like the interface element and code to make a link to drag the same intuition, using the right mouse button in page 1 to drag the link to page 2, and select the Model to do the link, Storyboard Segue will automatically link two UI Viewcontroller, such as.
Now you can use the simulator to see, in the Segue Demo page 1 Press the button to page 2, the screen will switch to page 2, if you want to use a different page change effect, you can change the Transition property in the Storyboard Segue, the example used is the flip Page Partial Curl effect.
Down is from page 2"back" Page one, here we specifically emphasize "return" rather than go, a wrong way, is to follow the above steps to drag page 2 button to link to the Model of page one, so that the entire Storyboard of the layout.
If you follow this design approach, you will find that two pages can really switch between each other, functionally correct, but the meaning is very different, and after multiple switching, it is possible to create too many uiviewcontroller entities, This makes the screen each time in the same direction to do the switching effect, each time will create a new Uiviewcontroller entity to use.
I think we should all guess. The correct approach should be to use dismissmodalviewcontrolleranimated in the code in Page 2: Method, or Dismissviewcontrolleranimated:completion: method to dissolve page 2 and return to the previous page.
Add a Uiviewcontroller subclass for the project, we name Page2viewcontroller, and cancel with the XIB for user interface (we have already created its interface), Then, in which you implement a button event to dissolve the page, the code is as follows.
C code
- -(Ibaction) Returntofirstpage: (ID) Sender {
- [Self Dismissviewcontrolleranimated:yes completion:^{}];
- }
Finally back to the project under the Storyboard, the new generation of our Uiviewcontroller and Page2viewcontroller to do the link, the way is to select the Uiviewcontroller attribute of the corresponding class file, such as, The button element is then linked to the Page2viewcontroller button event.
Now use the simulator to see, press the button on page 2, the screen will be in the opposite direction of the transition effect to return to page 1.
We have successfully built two uiviewcontroller that can be switched between each other, one through the Storyboard Segue, the other using the dismissviewcontrolleranimated: method to return to the previous Uiviewcontroller, the next is to solve the two uiviewcontroller between the value of the problem, here also provides two methods, in the section of page 1 also use Storyboard Segue to help us pass information to page 2, and in page 2 of the Section , it is to use the old method, through proxy delegate delegate way to transmit information.
First we pull a uitextfield element in two Uiviewcontroller, and link this element to class, respectively, named Page1textfield and Page2textfield, the way of linking can be referenced from the use UIButton says Hello starts with the text, and if you're a friend using Xcode 4, you can also refer to the Assistant editor associated editing feature in Xcode 4 to save time writing code.
In the Storyboard Segue, we must set a NSString variable in the Uiviewcontroller class of page 2, which is used to receive the funds transmitted by page 1 through Storyboard Segue. The code below.
C code
- @property (weak) NSString *string;
- Don't forget to add @synthesize string to the corresponding implementation file;
Then in the Viewdidload function we assign the value of string to Page2textfield, this action will allow page 2 to be opened when the screen will be set to the Page2textfield of the content of Storyboard Segue sent over the value.
C code
- -(void) Viewdidload
- {
- [Super Viewdidload];
- Page2textfield.text = string;
- }
Then go back to Page 1 of the Uiviewcontroller class, add a built-in function as follows, the completion of the Storyboard Segue pass the value of the method.
C code
- -(void) Prepareforsegue: (Uistoryboardsegue *) Segue Sender: (ID) Sender {
- Set the Page2 to the target of the storyboard segue Uiviewcontroller
- ID page2 = Segue.destinationviewcontroller;
- String variable that takes the value through storyboard segue to page 2
- [Page2 setValue:page1TextField.text forkey:@"string"];
- }
Another way is to use proxy delegate delegate method, through the establishment of a contract @protocol, so that the other class can be satisfied that the agreement is a function of the agreement, we set a contract in the Uiviewcontroller class on page 2, and In the Uiviewcontroller class of page 1, the method of implementing the contract, allowing the program to execute to page 2 o'clock, still be able to get the instance Instance of page 1, and then use the method in the contract to set the value of Page2textfield.
First comes to page 2 of the Uiviewcontroller class, establishes a contract page2delegate, and defines its internal method Passvalue:, and then declares an object that uses this agreement delegate, its code is as follows.
C code
- #import <UIKit/UIKit.h>
- Establish an agreement
- @protocol page2delegate
- Methods in the contract
- -(void) Passvalue: (NSString *) value;
- @end
- @interface Page2viewcontroller:uiviewcontroller
- @property (Weak, nonatomic) Iboutlet Uitextfield *page2textfield;
- @property (weak) NSString *string;
- Declaring an object with a page2delegate agreement.
- @property (weak) ID delegate;
- @end
The next step is to decide when the method in the contract will take effect, that is, where to call the Passvalue: Method function, which is implemented in the class class that uses the contract. Recalling the requirements of the program, we want to press the button on page 2 to return to page 1, the value of Page2textfield can be passed to page 1 of the Page1textfield, so the call contract Passvalue: Method code, is bound to write in this button event.
C code
- -(Ibaction) Returntofirstpage: (ID) Sender {
- [Self Dismissviewcontrolleranimated:yes completion:^{}];
- Call the method in the contract and bring the value into the Page2textfield
- [Delegate PassValue:page2TextField.text];
- }
Now, when we press the button on page 2, we call the class with the Page2delegate contract, and this class has to implement the method within this contract, so we go back to Uiviewcontroller class on page 1, we're going to use Pa for this class. Ge2delegate, and Passvalue within the agreement: a method function.
The contract is made by adding the < contract name > code in place of the @interface section, since the contract is written in another class, so don't forget to refer to it before using the contract, the following is the Uiviewcontroller class for page 1. h header File 。
C code
- #import <UIKit/UIKit.h>
- Referencing the class that holds the Page2delegate agreement
- #import "Page2ViewController.h"
- @interface Mlviewcontroller:uiviewcontroller //Use agreement
- @property (Weak, nonatomic) Iboutlet Uitextfield *page1textfield;
- @end
The Passvalue: Method function in the. m implementation file is then implemented in the contract.
C code
- -(void) Passvalue: (NSString *) value {
- Sets the number of Page1textfield to be taken
- Page1textfield.text = value;
- }
The final step, which you will often forget, is to set the proxy delegate to itself (page 1 of Uiviewcontroller), which is the most commonly written line of code object when you use the contract. Delegate = Self (object refers to page 2 of the Uiviewcontroller), as to where this line of code to write? Remember the previous Storyboard Segue, we have obtained the Uiviewcontroller instance Page2 of page 2 with the built-in function, so we modify this built-in function and use Page2 to set the delegate variable.
C code
- -(void) Prepareforsegue: (Uistoryboardsegue *) Segue Sender: (ID) Sender {
- Set the Page2 to the target of the storyboard segue Uiviewcontroller
- ID page2 = Segue.destinationviewcontroller;
- String variable that takes the value through storyboard segue to page 2
- [Page2 setValue:page1TextField.text forkey:@"string"];
- Set delegate to yourself (designate yourself as a proxy)
- [Page2 setvalue:self forkey:@"Delegate"];
- }
If this step is omitted, the delegate parameter in page 2 will not know who (which class) implemented its method when calling the Passvalue method, and therefore the parameter cannot be passed to page 1 by page 2.
PS: If you have a problem with the Uitextfield keyboard, you can refer to the method of closing the keypad at the end of the Uitextfield input.
"Xamarin Development IOS-switching with Storyboard Segue Uiviewcontroller (instance)"