OGeek|极客世界-中国程序员成长平台

标题: ios - 在 Objective-C 中使用 block 传递数据 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 14:54
标题: ios - 在 Objective-C 中使用 block 传递数据

我有两个 View Controller ,分别是 ViewControllerA 和 ViewControllerB。

在 ViewcontrollerB 上有 tableview 单元格。单击表格 View 单元格时,我想将在 ViewControllerB 上选择的数据发送到 ViewControllerA 上的标签。

我知道它可以通过多种方式实现,但是如何通过 block 来实现。请建议。

提前致谢!

View Controller B

-(void)tableViewUITableView *)tableView didSelectRowAtIndexPathNSIndexPath *)indexPath
{
    NSString *viewArr = [self.array objectAtIndex:indexPath.row];

    NSLog(@"the value obtained on cell is %@",viewArr);
    ViewController *vc=[[ViewController alloc]init];
    vc.aSimpleBlock(viewArr);   
}



Best Answer-推荐答案


在您的 ViewController A 中

1) 为 block 声明一个 typedef 说 typedef void (^simpleBlock)(NSString*);

2) 创建一个像

这样的 block 变量
@property(nonatomic,strong)simpleBlock  aSimpleBlock;

3)在viewDidAppear/viewDidLoad中定义这个 block

aSimpleBlock = ^(NSString* str){
        NSLog(@"Str is the string passed on tableView Cell Click..!!");
    };

在你的 ViewController B 你有 tableView 的地方

1)-(void)tableViewUITableView *)tableView didSelectRowAtIndexPathNSIndexPath *)indexPath

只需将您的 block 称为

your_VC_A_Object.aSimpleBlock("Your_Label_String_here");

关于ios - 在 Objective-C 中使用 block 传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42384215/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4