"Go" using Storyboard to achieve page jump, simple data transfer

Source: Internet
Author: User
Tags file handling

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
  1. -(Ibaction) pressed: (ID) Sender {
  2. [Self performseguewithidentifier:@ "second" sender:self];
  3. NSLog (@"Send message:%@", firstfield.text);
  4. //Page Jump Value method two: Using notification
  5. Nsdictionary *dicts = [nsdictionary dictionarywithobjectsandkeys:@"One1", @"one", @"Two2", @"one", @  "Three3", @"three", nil];
  6. //Registration (first step)
  7. Nsnotification *notification =[nsnotification notificationwithname:@"Mynotification" object:firstField.text];
  8. //Send (second step)
  9. [[Nsnotificationcenter Defaultcenter] postnotification:notification];
  10. //Registration + send can also be completed in one line (equivalent to the above two lines)
  11. [[Nsnotificationcenter Defaultcenter] postnotificationname:@"Mynotification2" object:dicts]; //Send a dictionary past
  12. }

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
  1. -(void) Viewdidload
  2. {
  3. [Super Viewdidload];
  4. additional setup after loading the view.
  5. //Accept-end: Accept (first step)
  6. [[Nsnotificationcenter Defaultcenter] addobserver:self selector:@selector (notificationhandler:) name:@"  Mynotification "Object:nil";
  7. [[Nsnotificationcenter Defaultcenter] addobserver:self selector:@selector (notificationHandler2:) name:@"  Mynotification2 "Object:nil";
  8. }
  9. Customizing the method of receiving information and processing (step two)
  10. -(void) Notificationhandler: (nsnotification *) notification{
  11. Secondfield.text = [Notification Object]; //Receive the message and show it in Uitextfield
  12. }
  13. Customizing the way to receive dictionary information
  14. -(void) NotificationHandler2: (nsnotification *) notification2{
  15. Nsdictionary *dict = [Notification2 object];
  16. NSLog (@"Receive dict:%@,forkey:%@", dict,[dict objectforkey:@"one"]);
  17. }

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. <1> Customizing a delegate for passing values
    2. @protocol viewpassvaluedelegate <NSObject>
    3. -(void) Passvalue:(  NSString *) value;
    4. @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
  1. #Import <UIKit/UIKit.h>
  2. #Import "ViewPassValueDelegate.h"
  3. @interface viewcontroller:uiviewcontroller{
  4. Nsobject<viewpassvaluedelegate> *delegte;
  5. }
  6. -(Ibaction) pressed: (ID) sender; //Main View button click on the action
  7. @property (Retain, nonatomic) Iboutlet Uitextfield *firstfield;
  8. @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
    1. -(Ibaction) pressed: (ID) Sender {
    2. Secondviewcontroller *secondcontroller = [[Secondviewcontroller alloc] init]; //Instantiate an object of view 2
    3. Delegte = Secondcontroller;
    4. [Delegte PassValue:firstField.text];
    5. }


4, and then in the Receive Data View (secondviewcontroller). h file implements the custom protocol

[Java]View Plaincopy
    1. <2> View 1 Implementation of custom protocols
    2. @interface secondviewcontroller:uiviewcontroller<viewpassvaluedelegate>


5, in the Receive Data View (Secondviewcontroller). m file implements the Passvalue method in the protocol:

[Java]View Plaincopy
    1. <3> ways to implement custom Delege
    2. -(void) Passvalue: (NSString *) value{
    3. secondfield.text= value;
    4. NSLog (@"Passvalue method get Value:%@ secondfield.text:%@", value,secondfield.text);
    5. }

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

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.