iOS Pass value--proxy value, block pass value, Nsnotificationcenter pass value

Source: Internet
Author: User

One: The use of proxy transmission value, is the use of agents to communicate.

Interface file:

#import <Foundation/Foundation.h>

@protocol cdelegate <NSObject>

-(void) Change: (NSString *) name;

@end

. h file

@interface viewcontroller:uiviewcontroller<cdelegate>

. m file

-(Ibaction) PUSHBB: (ID) Sender {

Bviewcontroller *bc=[[bviewcontroller alloc]initwithnibname:@ "Bviewcontroller" Bundle:[nsbundle MainBundle];

bc.delegate=self;

[Self PRESENTVIEWCONTROLLER:BC animated:yes completion:nil];

}

Bviewcontroller file

. h file

#import "ViewController.h"

#import "Cdelegate.h"

@interface Bviewcontroller:viewcontroller

@property (Weak, nonatomic) Iboutlet Uitextfield *name;

@property (nonatomic,assign) id<cdelegate> delegate;

@property (nonatomic,copy) Ablock block;

-(Ibaction) POPBB: (ID) sender;

@end

. m file

-(Ibaction) POPBB: (ID) Sender {

[Self.delegate Change:self.name.text];

[Self dismissviewcontrolleranimated:yes completion:nil];

}

II: Block Pass Value

typedef void (^ablock) (NSString *str);

@property (nonatomic,copy) Ablock block;

-(Ibaction) POPBB: (ID) Sender {

[Self.delegate Change:self.name.text];

Self.block (Self.name.text);

[Self dismissviewcontrolleranimated:yes completion:nil];

}

-(Ibaction) PUSHBB: (ID) Sender {

Bviewcontroller *bc=[[bviewcontroller alloc]initwithnibname:@ "Bviewcontroller" Bundle:[nsbundle MainBundle];

bc.block=^ (NSString *str) {

SELF.ANAME.TEXT=STR;

};

[Self PRESENTVIEWCONTROLLER:BC animated:yes completion:nil];

}

Three: Notice

Nsdictionary *dic=[nsdictionary dictionaryWithObject:self.name.text forkey:@ "name"];

[[Nsnotificationcenter defaultcenter]postnotificationname:@ "Changetext" object:self userInfo:dic];

-(void) Viewdidload {

[Super Viewdidload];

Additional setup after loading the view, typically from a nib.

[[Nsnotificationcenter defaultcenter]addobserver:self selector: @selector (changetext:) name:@ "Changetext" object: NIL];

}

iOS Pass value--proxy value, block pass value, Nsnotificationcenter pass value

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.