传值

通知中心传值

头文件
//  Common.h
//  通知中心传值
//

#ifndef Common_h
#define Common_h
#define NotificationName @"changeValur"

#endif /* Common_h */

------------------------------------------------------------------------------
//  ViewController.m
//  通知中心传值
//

#import "ViewController.h"
#import "Common.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *text1;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 通知的接受者(接收到通知后响应事件的对象) 接受到通知后响应的事件  当前通知的名称 当前通知的发送者(一般为nil)
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeValueText:) name:NotificationName object:nil];
    // Do any additional setup after loading the view, typically from a nib.
}
-(void)changeValueText:(NSNotification *)nsnotification{
    //获取通知发送者的发送的信息
    NSDictionary *dict = nsnotification.userInfo;
    //键值
    _text1.text = dict[@"value"];
}
-(void)dealloc{
    //移除通知
    [[NSNotificationCenter defaultCenter]removeObserver:self name:NotificationName object:nil];
}

--------------------------------------------------------------------------------
//  ChangeViewController.m
//  通知中心传值
//

#import "ChangeViewController.h"
#import "Common.h"
@interface ChangeViewController ()
@property (weak, nonatomic) IBOutlet UITextField *text2;

@end

@implementation ChangeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
- (IBAction)back:(UIButton *)sender {
  //发送通知的名称 发送者(一般为nil) 发送内容 [[NSNotificationCenter defaultCenter] postNotificationName:
object:nil userInfo:@{@"value":_text2.text}]; [self dismissViewControllerAnimated:YES completion:nil]; }

 代理传值

 

posted @ 2016-04-07 10:19  麦兜丶  阅读(148)  评论(0编辑  收藏  举报