ios-通知委托

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0) {
        // 委托
        [_delegate buyIphone:@"📱"];
    }else if (buttonIndex == 1){
    // 通知
        NSDictionary *dic = @{@"boom":@"💣"};
        [[NSNotificationCenter defaultCenter] postNotificationName:@"changeLabel" object:dic];
        
    }
}

通知

  • 第一个页面
    • 1.注册通知
[NSNotificationCenter defaultCenter] addObserver:self 
selector:@selector(justDoIt:) name:@"changeLabel" object:nil];
 *    2.拿到暗号,做事情(changeLabel为暗号)
    ```

-(void)justDoIt:(NSNotification *)obj{
NSDictionary *dic = [obj object];
_notificationOrigionLabel.text = dic[@"boom"];
}```

  • 第二个页面
    • 1:对暗号
    • 在button里写
        NSDictionary * dic = @{@"boom":@"💣"};
        [[NSNotificationCenter defaultCenter] postNotificationName:@"zhadan" object:dic];
 *  changeLabel为暗号  

委托

@protocol BuyIphone6sDelegate <NSObject>
-(void)buyIphone:(NSString *)str;
@property(nonatomic,strong)id<BuyIphone6sDelegate>delegate;
  • 【第二个页面】
  • 1在第二个页面写协议,写在interface 上面
    @protocol BuyIphone6sDelegate <NSObject>
  • 2.在第二个页面 实例化协议的变量
    -(void)buyIphone:(NSString *)str;
  • 3.让协议变量去做做协议中的方法
    @property(nonatomic,strong)id<BuyIphone6sDelegate>delegate;
  • 在button里实现
 UIAlertAction *enter = [UIAlertAction actionWithTitle:@"委托" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [_delegate buyIphone:@"📱"];
        }];
  • 方法
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0) {
        // 委托
        [_delegate buyIphone:@"📱"];
  • 【第一个页面】
    • 1.跳转页面的时候,签合同。

vc2.delegate = self; self为vc1

  • 2.在interface中实现这个协议
    @interface DPNViewController ()<BuyIphone6sDelegate>
  • 3.在.m中实现协议方法
-(void)buyIphone:(NSString *)str{
    
    _delegateOriginLabel.text = str;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 通知 第一个页面1.注册通知2.拿到暗号,做事情 ``` -(void)justDoIt:(NSNotificat...
    shanshan950224阅读 466评论 4 4
  • 通知 第一个页面1.注册通知 -(void)justDoIt:(NSNotification *)obj{NSDi...
    徐大拿阅读 140评论 0 0
  • 通知 第一个页面1.注册通知2.拿到暗号,做事情 ``` -(void)justDoIt:(NSNotificat...
    你瞅誰阅读 579评论 2 3
  • 通知 第一个页面1.注册通知2.拿到暗号,做事情 ``` -(void)justDoIt:(NSNotificat...
    小布丁儿阅读 169评论 0 0
  • 通知 第一个页面1.注册通知 -(void)justDoIt:(NSNotification *)obj{NSDi...
    王老板阅读 197评论 0 1