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.注冊通知
    • 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在第二個頁面寫協(xié)議,寫在interface 上面
    @protocol BuyIphone6sDelegate <NSObject>
  • 2.在第二個頁面 實例化協(xié)議的變量
    -(void)buyIphone:(NSString *)str;
  • 3.讓協(xié)議變量去做做協(xié)議中的方法
    @property(nonatomic,strong)id<BuyIphone6sDelegate>delegate;
  • 在button里實現(xiàn)
 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.跳轉(zhuǎn)頁面的時候,簽合同。

vc2.delegate = self; self為vc1

  • 2.在interface中實現(xiàn)這個協(xié)議
    @interface DPNViewController ()<BuyIphone6sDelegate>
  • 3.在.m中實現(xiàn)協(xié)議方法
-(void)buyIphone:(NSString *)str{
    
    _delegateOriginLabel.text = str;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 通知 第一個頁面1.注冊通知2.拿到暗號,做事情 ``` -(void)justDoIt:(NSNotificat...
    你瞅誰閱讀 579評論 2 3
  • 通知 第一個頁面1.注冊通知2.拿到暗號,做事情 ``` -(void)justDoIt:(NSNotificat...
    小布丁兒閱讀 169評論 0 0
  • 通知 第一個頁面1.注冊通知 -(void)justDoIt:(NSNotification *)obj{NSDi...
    徐大拿閱讀 143評論 0 0
  • 通知 第一個頁面1.注冊通知 -(void)justDoIt:(NSNotification *)obj{NSDi...
    Roger_單閱讀 331評論 0 0
  • 通知 第一個頁面1.注冊通知 -(void)justDoIt:(NSNotification *)obj{NSDi...
    王老板閱讀 199評論 0 1