iOS Learning Interface Communication

Source: Internet
Author: User

first, the value of the property passedin the SecondViewController.h.  
#import <UIKit/UIKit.h>@interface  Secondviewcontroller:uiviewcontroller
// The first step: Declare the property, used to store the previous page passed data @property (nonatomic, copy) NSString *contents; @end

  In the ROOTVIEWCONTROLLER.M.

-(void) loginaction{    *svc = [[Secondviewcontroller alloc] init];         // The second step is to assign the value    svc.contents = Self.rootView.userTextField.text;    [Self.navigationcontroller pushviewcontroller:svc animated:yes];}

  In the SECONDVIEWCONTROLLER.M.

-(void) viewdidload {    [super Viewdidload];     // Do any additional setup after loading the view.    @" Welcome " ;     = [Uicolor graycolor];        [Self initlayout];     // third step, show content    Self.nameLabel.text = self.contents;}
second, the protocol transmission value1, the agreement six steps
    • First step: declaring an agreement
    • Step two: Declare the agent
    • Step three: Execute protocol method
    • Fourth step: Sign an agreement
    • Fifth step: Appoint an agent
    • Sixth step: Implement the Protocol method
2. CodeFirst step: Declare the agreement in SecondViewController.h  
// 1. Defining the Agreement @protocol Secondviewcontrollerdelegate <NSObject>//  Declaration protocol Method -(void) ChangeValue: ( NSString *) name; @end

  Step two: Declare an agent in SecondViewController.h.

@interface Secondviewcontroller:uiviewcontroller // declarative proxies, semantic settings use assign to prevent circular referencing ID<SecondViewControllerDelegate>*contents; @end

  Step three: The method of using proxy call proxy in SECONDVIEWCONTROLLER.M

 // -(void ) Mybtnaction: (UIButton *) sender{ < span>  1. Upload the input content to the first page  //  step three: Use proxy to invoke the proxy's pass-through method  //  judge whether the agent implements the method to invoke the /span> if   ([self.seconddelegate    Respondstoselector: @selector (changevalue:)]) {[Self.seconddelegate changeValue:self.myField.text];  //  2. Jump back to the first page   [Self.navigationcontroller Popviewcontrolleranimated:yes];}  

  Fourth step: Signing an agreement in the extension of ROOTVIEWCONTROLLER.M

// Fourth Step: sign an Agreement @interface Rootviewcontroller () <SecondViewControllerDelegate>**mybtn; @end

  Fifth step: Specify the current object as a delegate in ROOTVIEWCONTROLLER.M

// Implementation Event -(void) rightaction{    *svc = [[Secondviewcontroller alloc] init];     // Fifth Step: Specify the current object as the delegate    Svc.seconddelegate = self ;    [Self.navigationcontroller pushviewcontroller:svc animated:yes];}

  Sixth step: Implement the Protocol method in ROOTVIEWCONTROLLER.M

// Sixth step: Implement protocol Method -(void) ChangeValue: (NSString *) name{    = name;}
third, block transmission value1. Review block
    • Block is an anonymous function capable of implementing function callback function
    • For communication between pages, and can be used to transmit values
void (^block) (void) = ^ (void) {}; // among them: // void (^block) (void) is a type // block is the variable name // ^ (void) {}; is block implementation // blocks that do not use local variables are stored in the global zone // block with local variables stored in the stack area
2, block transfer value
    • Use scenario-like protocol to pass value, is to solve from the next page forward a page value problem
3, block transmission value of two ways
    • Method One: Use the Block property to implement the callback pass value
    • Mode two: Define the block implementation callback value in the method

  DECLARE block in SecondViewController.h

@interface Secondviewcontroller:uiviewcontroller // First step: Define the Block property void (^block) (NSString *); @end

  Implement block in FIRSTVIEWCONTROLLER.M

-(void) mybtnaction{    *SECONDVC = [[Secondviewcontroller alloc] init];     // Step Two: Implement Block    Secondvc.block = ^void (NSString *textstring) {        = textstring;    };    [Self.navigationcontroller PUSHVIEWCONTROLLER:SECONDVC animated:yes];}

  block values in SECONDVIEWCONTROLLER.M

// implementation method for block pass value -(void) mybtnaction{    //  block pass value     self.block (self.myField.text);     // return to first page     [Self.navigationcontroller popviewcontrolleranimated:yes];}
Iv. block Memory management1. declare block in. h
#import <UIKit/UIKit.h>@interface secondviewcontroller:uiviewcontroller//void (^block) ( ); @end     

 2. Implement block in. m

    //Implement block/** * when you define a block property in a controller, if you need to use the current controller object in the block, you cannot use self directly, otherwise it will cause a circular reference */// use __weak adornments to prevent circular references // __weak Secondviewcontroller *SECONDVC = Self; __weak typeof (self) SECONDVC =void () {NSLog (@ " This is the implementation of block  "); SecondVC.view.backgroundColor = [Uicolor redcolor];}; // call block Self.block ();      

iOS Learning Interface communication

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.