美文网首页
oc中使用delegate和block进行反向传值

oc中使用delegate和block进行反向传值

作者: 鹏飞说 | 来源:发表于2018-02-28 14:21 被阅读18次

delegate传值

## 在BViewController中 
.h文件中  
@protocol BViewControllerDelegate<NSObject>

-(void)sendValue:(NSString *)value;

@end  


@property (nonatomic,strong)id<BViewControllerDelegate> delegate;

.m文件中  
if ([_delegate respondsToSelector:@selector(sendValue:)]) {
        [_delegate sendValue:@"string"];
    }

## 在AViewController中
首先实现代理<BViewControllerDelegate>  
实现bvc中的代理方法
    .delegate = self;
    
回调代理中的方法
-(void)sendValue:(NSString *)value {
    这里就获取到在代理中的数值
}

block实现传值

## 在BViewController中
在.h文件中
@property (nonatomic,copy) void(^PassMessage)(NSString *string);

.m文件中
self.PassMessage(@"string");

在AViewController中
.m文件中
    bvc.PassMessage = ^(NSString *string) {
        NSLog(@"%@", string);
    };

相关文章

网友评论

      本文标题:oc中使用delegate和block进行反向传值

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