码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2015-04-18 01:11:59      阅读:289      评论:0      收藏:0      [点我收藏+]

标签:

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

接口文件:

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

 

}

 

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

标签:

原文地址:http://www.cnblogs.com/varliny/p/4436436.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!