你的位置:首页 > 软件开发 > 操作系统 > IOS传值--代理传值,block传值,NSNotificationCenter传值

IOS传值--代理传值,block传值,NSNotificationCenter传值

发布时间:2015-04-18 04:00:19
一:利用代理传值,就是利用代理进行通信。接口文件:#import <Foundation/Foundation.h> @protocol Cdelegate <NSObject> -(void)change:(NSString *)name; ...

一:利用代理传值,就是利用代理进行通信。

接口文件:

#import <Foundation/Foundation.h>

 

@protocol Cdelegate <NSObject>

 

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

 

 

 

@end

 

.h文件

 

@interface ViewController : UIViewController<Cdelegate>

 

.m文件

- (IBAction)pushBB:(id)sender {

    BViewController *bc=[[BViewController alloc]initWithNibName:@"BViewController" bundle:[NSBundle mainBundle]];

    bc.delegate=self;

    [self presentViewController:bc animated:YES completion:nil];

 

}

 

BViewController文件

.h文件

#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文件

- (IBAction)popBB:(id)sender {

    [self.delegate change:self.name.text]; 

    [self dismissViewControllerAnimated:YES completion:nil];

}

二:block传值

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];

 

}

 

三:通知

 

   NSDictionary *dic=[NSDictionary dictionaryWithObject:self.name.text forKey:@"name"];

    

    [[NSNotificationCenter defaultCenter]postNotificationName:@"changeText" object:self userInfo:dic];

 

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

 

       [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeText:) name:@"changeText" object:nil];

 

}

 


 

海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com

原标题:IOS传值--代理传值,block传值,NSNotificationCenter传值

关键词:IOS

IOS
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。