委托与代理

2015-12-11  本文已影响22人  王老板
-(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];
        
    }
}

通知

[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"];
}```

        NSDictionary * dic = @{@"boom":@"💣"};
        [[NSNotificationCenter defaultCenter] postNotificationName:@"zhadan" object:dic];
 *  changeLabel为暗号  

委托

@protocol BuyIphone6sDelegate <NSObject>
-(void)buyIphone:(NSString *)str;
@property(nonatomic,strong)id<BuyIphone6sDelegate>delegate;
 UIAlertAction *enter = [UIAlertAction actionWithTitle:@"委托" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [_delegate buyIphone:@"📱"];
        }];
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0) {
        // 委托
        [_delegate buyIphone:@"📱"];

vc2.delegate = self; self为vc1

-(void)buyIphone:(NSString *)str{
    
    _delegateOriginLabel.text = str;
}
上一篇下一篇

猜你喜欢

热点阅读