iOS four ways to reverse-pass values

Source: Internet
Author: User

Method One: Use target-action design mode

The code is as follows: (the child view is rolled out by the root view, then the root view is rolled up, and when the root view is rolled out, the child view passes a color attribute to the root view to modify the background color of the root view)

Root View Controller code:

. m file-  (void) viewdidload{    [super viewdidload];         self.view.backgroundColor = [UIColor redColor];         [self createbutton];} -  (void) createbutton{    uibutton * btn = [uibutton  buttonwithtype:uibuttontypecustom];        btn.frame =  CGRectMake (10, 30, 300, 40);     [btn settitle:@ "Go to Next View controller"   forstate:uicontrolstatenormal];    btn.layer.cornerradius = 5;         btn.backgroundColor = [UIColor blackColor];         [btn addtarget:self action: @selector (btnclick)  forcontrolevents: Uicontroleventtouchupinside];        [self.view addsubVIEW:BTN];} -  (void) btnclick{    sub1viewcontroller * sub1 = [[ Sub1viewcontroller alloc]init];        sub1.view.backgroundcolor  = [UIColor blueColor];        sub1.target =  self;        sub1.action =  @selector (changeColor:);         [self presentViewController:sub1 animated:YES  Completion:nil];} -  (void) ChangeColor: (uicolor *) color{    self.view.backgroundcolor =  Color;}

Child View Code:

. h file @interface sub1viewcontroller : uiviewcontroller@property  (Assign,readwrite, nonatomic) id target; @property   (assign,readwrite,nonatomic) sel action; @end//.m file-  (void) viewdidload{    [super viewdidload];        [ SELF CREATEPOPTOROOTVIEWBTN];} -  (void) Createpoptorootviewbtn{    uibutton * btn = [uibutton  buttonwithtype:uibuttontypecustom];        btn.frame =  cgrectmake (10, 30, 300, 40);     [btn settitle:@ "Go to Root View Controller"   forstate:uicontrolstatenormal];    btn.layer.cornerradius = 5;         btn.backgroundColor = [UIColor blackColor];         [btn addtarget:self action: @selector (Btnclick)  forcontrolevents:uICONTROLEVENTTOUCHUPINSIDE];        [SELF.VIEW ADDSUBVIEW:BTN];} -  (void) btnclick{    if  ([_target respondstoselector:_action])      {        [_target performSelector:_action  withobject:[uicolor orangecolor]];    }    [self  Dismissviewcontrolleranimated:yes completion:nil];}

Method Two: Notifications

. m root View-  (void) viewdidload{    [super viewdidload];         self.view.backgroundColor = [UIColor redColor];         [self createbutton];        [[ Nsnotificationcenter defaultcenter] addobserver:self selector: @selector (changeColor:)  name : @ "ChangeColor"  object:nil];} -  (void) createbutton{    uibutton * btn = [uibutton  buttonwithtype:uibuttontypecustom];        btn.frame =  CGRectMake (10, 30, 300, 40);     [btn settitle:@ "Go to Next View controller"   forstate:uicontrolstatenormal];    btn.layer.cornerradius = 5;         btn.backgroundColor = [UIColor blackColor];      &nBsp;  [btn addtarget:self action: @selector (btnclick)  forcontrolevents: UICONTROLEVENTTOUCHUPINSIDE];        [SELF.VIEW ADDSUBVIEW:BTN];} -  (void) btnclick{    sub1viewcontroller * sub1 = [[ Sub1viewcontroller alloc]init];        sub1.view.backgroundcolor  = [UIColor blueColor];        [self  Presentviewcontroller:sub1 animated:yes completion:nil];} -  (void) ChangeColor: (nsnotification *) nofi{    self.view.backgroundcolor =  [nofi.userinfo objectforkey:@ "Color"];}

Child View Code

-  (void) viewdidload{    [super viewdidload];         [SELF CREATEPOPTOROOTVIEWBTN];} -  (void) Createpoptorootviewbtn{    uibutton * btn = [uibutton  buttonwithtype:uibuttontypecustom];        btn.frame =  cgrectmake (10, 30, 300, 40);     [btn settitle:@ "Go to Root View Controller"   forstate:uicontrolstatenormal];    btn.layer.cornerradius = 5;         btn.backgroundColor = [UIColor blackColor];         [[nsnotificationcenter defaultcenter]postnotificationname:@ "ChangeColor"  object:self userinfo:[nsdictionary dictionarywithobjectsandkeys:[uicolor orangecolor],@ " Color ",  nil]];        [btn addtarget:selF action: @selector (Btnclick)  forControlEvents:UIControlEventTouchUpInside];         [self.view addsubview:btn];        }-   (void) Btnclick{    [self dismissviewcontrolleranimated:yes completion:nil] ;}

Method Three: code block

Root View Code:

-  (void) viewdidload{    [super viewdidload];         self.view.backgroundColor = [UIColor redColor];         [self createbutton];} -  (void) createbutton{    uibutton * btn = [uibutton  buttonwithtype:uibuttontypecustom];        btn.frame =  CGRectMake (10, 30, 300, 40);     [btn settitle:@ "Go to Next View controller"   forstate:uicontrolstatenormal];    btn.layer.cornerradius = 5;         btn.backgroundColor = [UIColor blackColor];         [btn addtarget:self action: @selector (btnclick)  forcontrolevents: Uicontroleventtouchupinside];        [self.view addsubview:bTN];} -  (void) btnclick{    sub1viewcontroller * sub1 = [[ Sub1viewcontroller alloc]init];        sub1.view.backgroundcolor  = [uicolor bluecolor];        sub1. myblock = ^ (Uicolor * color)     {         self.view.backgroundColor = color;    };         [self presentviewcontroller:sub1 animated:yes completion:nil];}

Child View Code

. h file @interface sub1viewcontroller : uiviewcontroller@property  (copy,nonatomic,readwrite) void  (^myblock) (uicolor * color); @end//.m file-  (void) viewdidload{    [ SUPER VIEWDIDLOAD];        [SELF CREATEPOPTOROOTVIEWBTN];} -  (void) Createpoptorootviewbtn{    uibutton * btn = [uibutton  buttonwithtype:uibuttontypecustom];        btn.frame =  cgrectmake (10, 30, 300, 40);     [btn settitle:@ "Go to Root View Controller"   forstate:uicontrolstatenormal];    btn.layer.cornerradius = 5;         btn.backgroundColor = [UIColor blackColor];         [btn addtarget:self action: @selector (btnclick)  forcontrolevents: UIControlEventTouchUpInside];        [self.view addSubview:btn];         }-  (void) Btnclick{    _myblock ([Uicolor orangecolor]);     [self dismissviewcontrolleranimated:yes completion:nil];}

Method Four: Agent-protocol

Root View Code

.h#import <uikit/uikit.h> #import   "Sub1ViewController.h" @interface  viewcontroller :  UIViewController<Sub1ViewControllerDelete> @end//.m-  (void) viewdidload{     [super viewDidLoad];        self.view.backgroundColor  = [uicolor redcolor];        [self createbutton];} -  (void) createbutton{    uibutton * btn = [uibutton  buttonwithtype:uibuttontypecustom];        btn.frame =  CGRectMake (10, 30, 300, 40);     [btn settitle:@ "Go to Next View controller"   forstate:uicontrolstatenormal];    btn.layer.cornerradius = 5;         btn.backgroundColor = [UIColor blackColor];         [btn adDtarget:self action: @selector (Btnclick)  forControlEvents:UIControlEventTouchUpInside];   &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;[SELF.VIEW&NBSP;ADDSUBVIEW:BTN];} -  (void) btnclick{    sub1viewcontroller * sub1 = [[ Sub1viewcontroller alloc]init];        sub1.view.backgroundcolor  = [UIColor blueColor];    sub1.delegate = self;     [self presentviewcontroller:sub1 animated:yes completion:nil];} -  (void) ChangeColor: (uicolor *) color{    self.view.backgroundcolor =  Color;}

Child View Controller

. h file #import <uikit/uikit.h> @protocol  Sub1ViewControllerDelete <NSObject>-  ( void) ChangeColor: (uicolor *) color; @end @interface sub1viewcontroller :  uiviewcontroller@property  (Assign,nonatomic,readwrite) id <sub1viewcontrollerdelete>delegate;@ END//.M file-  (void) viewdidload{    [super viewdidload];     &NBSP;&NBSP;&NBSP;&NBSP;[SELF&NBSP;CREATEPOPTOROOTVIEWBTN];} -  (void) Createpoptorootviewbtn{    uibutton * btn = [uibutton  buttonwithtype:uibuttontypecustom];        btn.frame =  cgrectmake (10, 30, 300, 40);     [btn settitle:@ "Go to Root View Controller"   forstate:uicontrolstatenormal];    btn.layer.cornerradius = 5;         btn.backgroundcolor = [uicolor blackcolor]; &Nbsp;      [btn addtarget:self action: @selector (BtnClick)   forcontrolevents:uicontroleventtouchupinside];        [self.view  addsubview:btn];        }-  (void) btnclick{     [_delegate changeColor:[UIColor orangeColor]];         [Self dismissviewcontrolleranimated:yes completion:nil];}


This article is from the Web Learning Summary blog, so be sure to keep this source http://8947509.blog.51cto.com/8937509/1586745

iOS four ways to reverse-pass values

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.