IOS 通知与委托

作者: shanshan950224 | 来源:发表于2015-12-11 13:40 被阅读225次

-(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.注册通知
    • 2.拿到暗号,做事情
     ```
    

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

  • 第二个页面
    • 1:对暗号
    • 在button里写
 UIAlertAction *cancle = [UIAlertAction actionWithTitle:@"通知" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [ac addAction:cancle];
        NSDictionary * dic = @{@"boom":@"💣"};
        [[NSNotificationCenter defaultCenter] postNotificationName:@"zhadan" object:dic];
 *   changeLabel为暗号  

委托

  • 【第二个页面】
  • 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;
}

相关文章

  • IOS 通知与委托

    通知 第一个页面1.注册通知2.拿到暗号,做事情 ``` -(void)justDoIt:(NSNotificat...

  • 委托与通知

    objective-c it分类:IOS 委托(Delegation) 委托是一种设计模式。这种模式虽然简单但是功...

  • 通知与委托

    通知 第一个页面1.注册通知 -(void)justDoIt:(NSNotification *)obj{NSDi...

  • 通知与委托

    通知 第一个页面1.注册通知 2.拿到暗号,做事情(changeLabel为暗号) 第二个页面1:对暗号在butt...

  • 委托与通知

    通知 第一个页面1.注册通知2.拿到暗号,做事情 ``` -(void)justDoIt:(NSNotificat...

  • iOS 系统委托通知

    https://www.douban.com/note/401093498/

  • ios-通知委托

    通知 第一个页面1.注册通知 -(void)justDoIt:(NSNotification *)obj{NSDi...

  • 委托通知与代理

    通知 第一个页面1.注册通知2.拿到暗号,做事情 ``` -(void)justDoIt:(NSNotificat...

  • 常用设计模式整理

    ios面试题(整理) ios常用的设计模式有哪些? MVC模式、委托模式、观察者模式(kvo、kvc、通知机制)、...

  • block

    block源码在iOS中消息通信一般有委托(delegate)、通知(NSNotification)、KVO以及b...

网友评论

  • 笑看风云xxx:楼主 iOS SDK 中几乎所有的委托都是弱引用属性weak,这是为了避免对象及其delegate之间产生强引用循环。
    不要用strong
    @property(nonatomic,strong)id<BuyIphone6sDelegate>delegate;
  • 最爱东山哥丶:假如是用导航跳转界面的话可以直接使用代理跳转,而不需要重新写一个协议。
  • 华南犀牛:楼主叫代理干活的时候,少做了预防性编程。。。if (self.delegate responxxxxxxxxxx) 印象中是这样,最近不怎么用,也快忘了

本文标题:IOS 通知与委托

本文链接:https://www.haomeiwen.com/subject/rtyyhttx.html