NSNotificationCenter

来源:互联网 发布:淘宝支点运动是真的吗 编辑:程序博客网 时间:2024/05/01 16:25


//MRC机制

-(void)dealloc

{

    //移除通知

    //1 暴力型

    [[NSNotificationCenter defaultCenter]removeObserver:self];

    //2 柔和型的 移除某一消息的标示

    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"123" object:nil];

    

}

- (void)viewDidLoad {

    [super viewDidLoad];

    //1 注册通知

    //有通知中心提供的接口注册

    //拿到通知中心对象接口[NSNotificationCenter defaultCenter]

    /**

     *1 observer 观察者 及接收通知的对象 self

     2 selector 就收通知之后要做的事  方法名如果再有参数 参数类型是

     消息载体 NSNotification 类型的

     3 name 消息的机制

     4 object 发送消息的对象 一般是nil

     */

    //1 注册通知

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

    

    //创建按钮

    [self creatButton];

    

}

//创建一个按钮

-(void)creatButton

{

    UIButton*button=[UIButton buttonWithType:UIButtonTypeCustom];

    

    button.frame=CGRectMake(10010010030);

    

    button.backgroundColor=[UIColor orangeColor];

    

    [button setTitle:@"点我给福利" forState:UIControlStateNormal];

   

    

    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];

    

    [self.view addSubview:button];

}

//button 的事件

-(void)buttonClick:(UIButton*)button

{

    /**

     * 发送机制

     复杂的

     先创建一个消息载体

     1 name 消息的标示  这里的标示 一定要和注册标示 一致 注册者才能收到 此类的标示消息

     2 object 发送消息的对象

     3 userInfo 消息的内容 字典类型

     */

  //  NSNotification*noti=[NSNotification notificationWithName:@"123" object:nil userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor greenColor],@"color", nil]];

    

    NSLog(@"转到第二页 并开始发送消息1");

    

//    SesondViewController*fvc=[[SesondViewController alloc]init];

//    

//    

//    

//    /**

//     *  重点是CATransition 重点是CATransition 重点是CATransition

//     */

//    CATransition*animation=[CATransition animation];

//    [self.view.layer addAnimation:animation forKey:nil];

//    animation.duration=3;

//    [self presentViewController:fvc animated:YES completion:^{

//        //NSLog(@"跳转完成!");

//    }];

 

    

    

    NSString*str=@"41_6.jpg";

    NSNotification*noti=[NSNotification notificationWithName:@"123" object:nil userInfo:[NSDictionary dictionaryWithObjectsAndKeys:str,@"name"nil]];

    //发送消息

    [[NSNotificationCenter defaultCenter]postNotification:noti];

//

    

    NSLog(@"转到第二页 并开始发送消息2");

    //简单的

   // [NSNotificationCenter defaultCenter]postNotificationName:<#(NSString *)#> object:<#(id)#> userInfo:<#(NSDictionary *)#>

    

}

//接收通知的之后要做的事

-(void)notification:(NSNotification*)noti

{

    SesondViewController*fvc=[[SesondViewController alloc]init];

    /**

     *  重点是CATransition 重点是CATransition 重点是CATransition

     */

    

    UIImageView*backImage=[[UIImageView alloc]initWithFrame:CGRectMake(00320660)];

    //获取消息的内容

    backImage.userInteractionEnabled=YES;

    NSDictionary*dict=noti.userInfo;

    backImage.image=[UIImage imageNamed:dict[@"name"]];

    [fvc.view addSubview:backImage];

    NSLog(@"消息发送成功1");

    

    [backImage addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(TapGesture:)]];

    

    

    

    CATransition*animation=[CATransition animation];

     animation.duration=2;

    animation.subtype=@"fromLift";

    [self.view.layer addAnimation:animation forKey:nil];

    [self presentViewController:fvc animated:YES completion:^{

        //NSLog(@"跳转完成!");

    }];

    NSLog(@"消息发送成功2");

    

    //接收到消息之后开始执行的任务(把当前的页面改变背景)

   

    

    //[self.view addSubview:backImage];

    //self.view.backgroundColor=dict[@"color"];

    //NSLog(@"%@",dict[@"name"]);

}

-(void)TapGesture:(UITapGestureRecognizer*)tap

{

    [self dismissViewControllerAnimated:YES completion:^{

        //NSLog(@"返回成功ing");

    }];

 

    

}

0 0
原创粉丝点击