Having recently come into contact with iOS, Apple has suggested storyboard to build all the interfaces, so I followed the fashion and started using storyboard directly. (unexpectedly in the context of the page jump, encountered the problem is: No response after the click) well-known, in the storyboard, the interface of the jump is through the segue to achieve, using it, eliminating the method to write jump code.
One view jump
"View Jump under Storyboard"
We know: There are three types of segue: Push,modal, and Custom. such as://01
Simply say these three functions: use the navigation bar to press into the new controller (push), the modal load View controller (modal), the customization (custom).
Well, nonsense less, now start our trip.
1, first set up a single View template project, remember to check the storyboard. Then open it and add some labels and a button to Rootviewcontroller (that is, our main view).
2. Drag a viewcontroller into the library on the right and add a label. Specific as shown://02
3, select button, right (or control+ left mouse button) Drag to the second Viewcontroller, select: Modal, then remember save. This time, run the simulator, click button, you will find the success jump to the second interface. We didn't do anything in the code area, and even the button and the second Viewcontroller were not created, which is really simple. 03
Okay, here we go. Storyboard, using the Segue interface jump a total of two ways:
The first is the above example, using the button component, drag-and-drop add segue, run directly can be used.
The second is the use of Viewcontroller and Viewcontroller, drag and drop to add segue. However, this method needs to write the code within the corresponding method to jump, manually set its jump.
4, put just the example set button segue Delete (right button, point x), all restore the original state, we add a click to Buttom, and then implement this method in VIEWCONTROLLER.M, write such code in the method body://04
5, pay attention to the method parameter: @ "Second", this second is my self-naming an identifier, you will encounter.
6,save Save, open Storyboard, select Rootviewcontroller, right-drag to the second Viewcontroller, in the popup screen also select: Modal. 05
7. Open the Settings page for this segue: set its identifier to second, which is the self-named parameter in my code.
8,save Save, run the simulator, you will be amazed to find that the same jump was achieved.
So far, we've implemented the view jump in two simple ways: 1 is the segue,2 that sets the button is the segue between Viewcontroller and Viewcontroller, but the latter needs to manually manage the jump in the code.
Seemingly very simple things, but let me delay some time, mainly because I see a lot of examples on the internet is to Uinavigationcontroller for Rootviewcontroller (so save time, You can also use the return button that the system has created for us to return to Rootviewcontroller), and then drag the button to the second view with the push selected, because I did not understand the type meaning of the push, so I always choose push when I write, Created after the click cannot jump. Now finally clear, recorded, for the students do not understand the study.
----------------------------------------------------------------------------------------------------
According to Segue identifier Jump interface
[Self performseguewithidentifier:@ "gototwo" sender:self];
Jump in modal mode
[Self presentmodalviewcontroller:nil animated:yes];
Pressed into a Viewcontroller
[Self.navigationcontroller Pushviewcontroller:nil Animated:yes];
Pop up a viewcontroller quite with back to the previous interface
[Self.navigationcontroller Popviewcontrolleranimated:yes];
Return method with modal jump
[Self dismissmodalviewcontrolleranimated:yes];
-----------------------------------------------------------------------------------------------------
Then write about the segue three types of explanations:
--------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------
There are several different types of segue in storyboard, and the type of segue is different in the development of iphone and ipad.
in the iphone, Segue has: Push,modal, and custom three different types, these types of differences in the way that new pages appear.
and in the ipad, there are five different types of push,modal,popover,replace and custom.
modal
The most commonly used scene, the new scene completely covers the old one. Users can no longer interact with the previous scene unless they close the scene first. The
is the standard way of switching in Viewcontroller, including fading out what you can choose to switch animations.
Modalview: It will pop up a view, you can only operate on the view, and cannot switch to other view, unless you close the Modalview. The segue type that corresponds to
Modal view is Modal segue.
*modal:transition to another scene for the purposes of completing a task. When user is done in the modalview that pops up, it should be dismiss the Modal View scene and then switch back to the Originalview.
Push
The push type generally requires the first interface to be a navigation controller.
Is the kind of way to use the right-hand side of the navigation View controller in the lower level.
*push:create a chain of scenes where the user can move forward or back. The segue type is used with navigation viewcontrollers.
PopOver (IPad only)
PopOver type, is to use the form of floating window to display the new page
*popover (IPad only):D isplays the scene in a pop-up "windows" over top of the current view.
Replace (IPad only):
Replace the current scene,
Replace the current scene with another. This was used in some specialized iPad viewcontrollers (e.g. Split-view Controller).
Custom
is the custom jump mode.
*custom:used for programming a customtransition between scenes.
Using the custom Segue type in storyboard
Reference http://ryan.easymorse.com/?p=72
--------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------
"View Jump under Xib"
Now say, do not use storyboard, directly create xib when the page jump, in fact, is also very simple, as long as the understanding, is not a problem. I've just turned from Java and I didn't feel comfortable at first, but now I find interface builder really is too powerful.
1, create a project, I use the empty application template, this template created by the project contains only one Appdelegate.h and APPDELEGATE.M files, Rootviewcontroller need to create our own. (Note: In the latest version, Apple has canceled the Mainwindow.xib file so that it cannot open xib to view the included icons) the Run simulator will only display a blank interface. Okay, file-newfile. Create a Object-c class, open, subclass of Select the default Uiviewcontroller, note that you need to tick on with the XIB for user interface, Otherwise, you won't be able to create a main view.
2. Once created, open its Xib file and simply add some button components. The Rootviewcontroller that we created is still not available to save the running program at this time. "Sometimes if you select this Mainviewcontroller from the user interface under the project list, the background will also be reported: applications is expected to having a root view controller at the E ND of application launch error because the delegate of the project cannot find a Rootviewcontrller "so this requires us to manually associate them in Appdelegate.
AppDelegate.h
Appdelegate.m
[Self.viewcontroller is the one we declared in the. h file]
3, OK, after saving, this time will be able to display our interface is also very simple! Well, now we're going to create a secondviewcontroller (remember to tick xib), and then we'll add an event method to the button for the Rootviewcontroller view, You can make it click and jump to Secondviewcontroller.
(First create a secondviewcontroller and instantiate, intitwithnibname parameters must correctly write the name of the controller paired xib file, call Presentmodalviewcontroller: The controller will be able to jump. )
found that, in fact, are quite simple, could have created a single View application template, so save the creation of Rootviewcontroller and write those code in delegate, I am so that is let us feel, Xib, The difference between Delagate,stroyboard and the connection, all try, you will understand the mechanism between them, at least can write interface and jump method. Haha, what are the problems we communicate together, I am also a beginner, can give me a message oh. ~~~
Two page pass value
Method one uses segue to pass data, continuing the project example above.
1, declare a Uitextfield in Rootviewcontroller and associate with storyboard. File-add adds a secondviewcontroller (inherit Uiviewcontroller), and then sets its class association in the keyboard property of the second Viewcontroller. Also declare a Uitextfield and associate in Secondviewcontroller. ://06
2, then overwrite the name in the rootviewcontroller.m file: Prepareforsegue: (Uistoryboardsegue *) Segue Sender: (ID) Sender method, and write the following statement,//07
3, we get the main view of the text box content, and through the Segue method sent out, the next thing to do is to declare a @property NSString * type, the name of the data string, The following code is then written in the-(void) Viewdidload method in its. m file://08
Note: When sending data, [send setvalue:msg forkey:@ "data"]; This "data" name must be the same as the declaration of the post-jump interface of the type object name, otherwise, the interface after the jump will not receive the value passed.
Method Two, use notification broadcast to implement the view jump pass data, continue the above project expansion.
The broadcasting mechanism is divided into: register----send------------receive (receiver), see the code for details.
1, in the view page where you want to send the data. m file handling Send Logic method register + send
[Java]View Plaincopy
- -(Ibaction) pressed: (ID) Sender {
- [Self performseguewithidentifier:@ "second" sender:self];
- NSLog (@"Send message:%@", firstfield.text);
- //Page Jump Value method two: Using notification
- Nsdictionary *dicts = [nsdictionary dictionarywithobjectsandkeys:@"One1", @"one", @"Two2", @"one", @ "Three3", @"three", nil];
- //Registration (first step)
- Nsnotification *notification =[nsnotification notificationwithname:@"Mynotification" object:firstField.text];
- //Send (second step)
- [[Nsnotificationcenter Defaultcenter] postnotification:notification];
- //Registration + send can also be completed in one line (equivalent to the above two lines)
- [[Nsnotificationcenter Defaultcenter] postnotificationname:@"Mynotification2" object:dicts]; //Send a dictionary past
- }
Notificationwithname: The value of the parameter is defined by itself, and the receiver is called the receive identity by this name.
2, after the jump, receive Data View page. m file in the method of processing logic to receive
[Java]View Plaincopy
- -(void) Viewdidload
- {
- [Super Viewdidload];
- additional setup after loading the view.
- //Accept-end: Accept (first step)
- [[Nsnotificationcenter Defaultcenter] addobserver:self selector:@selector (notificationhandler:) name:@" Mynotification "Object:nil";
- [[Nsnotificationcenter Defaultcenter] addobserver:self selector:@selector (notificationHandler2:) name:@" Mynotification2 "Object:nil";
- }
- Customizing the method of receiving information and processing (step two)
- -(void) Notificationhandler: (nsnotification *) notification{
- Secondfield.text = [Notification Object]; //Receive the message and show it in Uitextfield
- }
- Customizing the way to receive dictionary information
- -(void) NotificationHandler2: (nsnotification *) notification2{
- Nsdictionary *dict = [Notification2 object];
- NSLog (@"Receive dict:%@,forkey:%@", dict,[dict objectforkey:@"one"]);
- }
Note: If the registered notification is not received in the target view or the name is incorrectly written, the relevant method of the target view will not be executed
Foreign Reference Forum "portal" http://blog.isotoma.com/2009/11/on-objective-c-delegates-and-nsnotification-objects/
Method Three, passing data by delegate delegate
This method after I finished testing, the feeling is not too good, there are some limitations, equivalent to the custom Read method: Nothing but a pointer to a B object in a object, and then in a function to set a B object a property value.
Concrete look at the tutorial put.
1, first add a file--objective-c protocol, and then declare a method to pass the value:
[Java]View Plaincopy
- <1> Customizing a delegate for passing values
- @protocol viewpassvaluedelegate <NSObject>
- -(void) Passvalue:( NSString *) value;
- @end
2, and then declare a custom delegate under the. h file of the view where you want to send the data.
[Java]View Plaincopy
- #Import <UIKit/UIKit.h>
- #Import "ViewPassValueDelegate.h"
- @interface viewcontroller:uiviewcontroller{
- Nsobject<viewpassvaluedelegate> *delegte;
- }
- -(Ibaction) pressed: (ID) sender; //Main View button click on the action
- @property (Retain, nonatomic) Iboutlet Uitextfield *firstfield;
- @end
3, declare a Secondviewcontroller instance in the event-handling method of the view to send the data, then assign the value to delegate, and execute the Passvalue method of the protocol.
[Java]View Plaincopy
- -(Ibaction) pressed: (ID) Sender {
- Secondviewcontroller *secondcontroller = [[Secondviewcontroller alloc] init]; //Instantiate an object of view 2
- Delegte = Secondcontroller;
- [Delegte PassValue:firstField.text];
- }
4, and then in the Receive Data View (secondviewcontroller). h file implements the custom protocol
[Java]View Plaincopy
- <2> View 1 Implementation of custom protocols
- @interface secondviewcontroller:uiviewcontroller<viewpassvaluedelegate>
5, in the Receive Data View (Secondviewcontroller). m file implements the Passvalue method in the protocol:
[Java]View Plaincopy
- <3> ways to implement custom Delege
- -(void) Passvalue: (NSString *) value{
- secondfield.text= value;
- NSLog (@"Passvalue method get Value:%@ secondfield.text:%@", value,secondfield.text);
- }
At this point, the printout will show you the data sent in the first interface, but I assign this data to the Uitextfield object of the page, but the value of this object is always null, cannot be displayed on the interface, I am also very distressed, what methods have been tried, It is found that this receive method will always execute after the Viewdidload method, and the method body setting Uitextfield any value cannot succeed.
The current summary is that the value above is just a pointer reference to the value assigned to the 1 object, and the method scope is invalid. So in the method of the body printing is data, out of the method body, there is no reference to hold.
Transferred from: http://blog.csdn.net/mad1989/article/details/7919504
"Go" using Storyboard to achieve page jump, simple data transfer