ios 通知的使用

ios通知的使用

http://www.cnblogs.com/heyonggang/p/3681689.html

ios开发系列-通知与消息机制

http://www.cnblogs.com/kenshincui/p/4168532.html#notificationCenter


通知的使用:

1.创建一个通知对象:使用notificationWithName:object: 或者 notificationWithName:object:userInfo:

2.注册通知:addObserver:selector:name:object:

3.发送通知:postNotificationName:object:或者performSelectorOnMainThread:withObject:waitUntilDone:

通知特点是一对多,不需要知道对方是谁

代理是一对一

例如:在第二个窗口文本框输入的值传入到第一个窗口中

代码:

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    UIStoryboard *sb= [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    ViewController *vc= [sb instantiateViewControllerWithIdentifier:@"main"];
    UINavigationController *nc= [[UINavigationController alloc] initWithRootViewController:vc];
    self.window.rootViewController= nc;
    [self.window makeKeyAndVisible];
    return YES;
}


#import "ViewController.h"
#import "nextViewController.h"

@interface ViewController ()//
@property (nonatomic, strong)UIButton *btn;
@property(nonatomic, strong)UILabel *ll;
//@property(nonatomic, strong)nextViewController *nvc;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.btn= [[UIButton alloc] initWithFrame:CGRectMake(40, 40, 110, 110)];
    [self.btn setBackgroundColor:[UIColor blueColor]];
    [self.btn addTarget:self action:@selector(hehe) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.btn];
    
    self.ll= [[UILabel alloc] initWithFrame:CGRectMake(200, 200, 110, 20)];
    self.ll.text= @"等待";
    [self.view addSubview:self.ll];
    
    //注册通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(act:) name:@"tongzhi" object:nil];
    //表示观察者为self 收到"tongzhi"通知时会执行act:方法 object参数是表示会对哪个发送者对象发出的事件作出响应,nil 时表示接受所有发送者的事件。
}

-(void)hehe{
    UIStoryboard *sb= [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    nextViewController *nvc2= [sb instantiateViewControllerWithIdentifier:@"two"];
//    nvc2.hope= ^(NSString *str){
//        self.ll.text= str;
//    };
//    NSLog(@"%@ %@",nvc2,nvc2.hope);
    [nvc2 addbtn];
    [self.navigationController pushViewController:nvc2 animated:YES];
}

-(void)act:(NSNotification *)text{
    NSLog(@"我接到通知了");
    self.ll.text= text.userInfo[@"textone"];
}

//-(void)afterClick:(NSString *)str{
//    self.ll.text=str;
//    NSLog(@"str:%@",str);
//}


@end


#import "nextViewController.h"
#import "ViewController.h"
@interface nextViewController ()

@end

@implementation nextViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.tf= [[UITextField alloc] initWithFrame:CGRectMake(20, 200, 200, 20)];
    [self.tf setBorderStyle:UITextBorderStyleLine];
    [self.view addSubview:self.tf];
}

-(void)addbtn{
    
    self.btn= [[UIButton alloc]initWithFrame:CGRectMake(50, 50, 100, 100)];
    [self.btn setBackgroundColor:[UIColor redColor]];
    [self.btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.btn];
}

//-(void)click{
//    NSLog(@"::%@ %@",self, self.hope);
//    Myblock block= self.hope;
//    if(block)
//        block(self.tf.text);
//    [self.delegate afterClick:self.tf.text];
    [self.delegate afterClick:@"蛤蛤蛤蛤蛤蛤蛤蛤蛤蛤"];
//    [self.navigationController popViewControllerAnimated:YES];
//}




-(void)click{
    NSLog(@"发出通知");
    NSDictionary *dict= [[NSDictionary alloc]initWithObjectsAndKeys:self.tf.text,@"textone",nil];
    //创建通知
    NSNotification *notification =[NSNotification notificationWithName:@"tongzhi" object:nil userInfo:dict];
    //通知中心发送通知
    [[NSNotificationCenter defaultCenter] postNotification:notification];
    [self.navigationController popViewControllerAnimated:YES];
}


@end



你可能感兴趣的:(iOS开发)