Ios 在Objective-C中使用块传递数据

Ios 在Objective-C中使用块传递数据,ios,objective-c,uitableview,objective-c-blocks,Ios,Objective C,Uitableview,Objective C Blocks,我有两个ViewController,分别是ViewControllerA和ViewControllerB 在ViewcontrollerB上有tableview单元格。单击tableview单元格后,我想将ViewControllerB上选择的数据发送到ViewControllerA上的标签 我知道这可以通过很多方式实现,但如何通过积木来实现呢?敬请建议 提前谢谢 视图控制器B -(void)tableView:(UITableView *)tableView didSelectRowAtIn

我有两个ViewController,分别是ViewControllerA和ViewControllerB

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

我知道这可以通过很多方式实现,但如何通过积木来实现呢?敬请建议

提前谢谢

视图控制器B

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

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

我不知道这是明智的还是可能的


为了在视图控制器之间直接传递数据,我经常使用委托。我将向B添加一个委托属性,并让它定义一个委托协议。我将有一个实现该协议并让B调用这些协议方法来传递数据。当然,将B的委托设置为A。

在这种情况下,您必须从
ViewControllerA
提供一个块,以便另一个(
ViewControllerB
)可以处理它(作为属性),保留它并在选择表行时调用它


但这对代表团有什么好处呢?事实上,在这种情况下,委派更像是一种标准模式。

在ViewController a中

1)为块声明一个typedef,比如
typedef void(^simpleBlock)(NSString*)

2)创建块变量,如

@property(nonatomic,strong)simpleBlock  aSimpleBlock;
3)在ViewDidDisplay/viewDidLoad中定义此块

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

在具有tableView的ViewController B中

1)
-(void)tableView:(UITableView*)tableView中选择了rowatindexpath:(nsindepath*)indepath

只需将您的
称为

your_VC_A_Object.aSimpleBlock("Your_Label_String_here");

您可以在viewControllerB中定义一个块并将其设置为ViewControllerA,当选择一个单元格时,您可以调用此块并将值传递给它,例如:
在viewControllerB中

// ViewControllerB.h
@interface ViewControllerB : UITableViewController

@property (nonatomic, copy) void (^didSelectCellBlock)(id obj);
@end

// ViewControllerB.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        NSString *str = [self.dataArray objectAtIndex:indexPath.row];
        if (self.didSelectCellBlock) {
            self.didSelectCellBlock(str);
        }
        ...
    }
}
和视图控制器A中的

 ViewControllerB *controllerB = [[ViewControllerB alloc] init];
 __weak __typeof(self) weakSelf = self;
 controllerB.didSelectCellBlock = ^(id obj) {
     weakSelf.label.text = (NSString *)obj;
 };

步骤1

最初将VC-B的父类添加到VC-A

@interface ViewControllerA : ViewControllerB
步骤2

ViewControllerB

@interface ViewControllerB : UIViewController

-(void)shareContent:(NSString*)currentText;
步骤3

在该ViewControllerB实现文件上

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {
     __weak typeof(self) weakSelf = self;

      dispatch_async(dispatch_get_main_queue(), ^ {

         [weakSelf shareContent: [_items objectAtIndex:indexPath.row]];

    });


}
 -(void)shareContent:(NSString*)currentText
 {

   NSLog(@"Local details:%@", currentText);
 }
步骤4

在ViewControllerA实现文件上

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {
     __weak typeof(self) weakSelf = self;

      dispatch_async(dispatch_get_main_queue(), ^ {

         [weakSelf shareContent: [_items objectAtIndex:indexPath.row]];

    });


}
 -(void)shareContent:(NSString*)currentText
 {

   NSLog(@"Local details:%@", currentText);
 }
选择-2

另一种方法是,您可以从或获取样本


在块中,我们也可以用多种方式进行int,你能告诉我你尝试过的代码吗?我没有通过块实现它,我已经用委托完成了…但现在我想通过bocks实现同样的目标…但无法理解正确的实现方式。好的,我添加了一些示例,您也可以使用NSNotifications。是的,这是可能的,甚至是可行的,因为块比委托快,也使用块,我们可以内联执行,使代码更可读。但同样正确的是,不能到处用块替换委托,有时委托是必不可少的,而不是块。当执行以下行时,会出现EXC_BAD_访问异常:your_VC_A_Object.aSimpleBlock(“your_Label_String_here”)@TestShroff您正在那里创建新对象。无需创建新对象,只需从导航到VC B的位置传递对象引用。创建新实例时,它将创建新的块变量,因此您将无法引用同一块。@TestShroff欢迎。。如果这解决了您的问题,请将其标记为正确答案,以便其他成员可以发现它有用。快乐的编码。。!!“只需从导航到VCB的位置传递对象引用。”请再解释一下。假设您正在从VC1导航到VC2。然后在VC2中创建VC1变量(但不要使用alloc init初始化它)。现在,当您要推送VC2时,请这样做:VC2_object.VC1_Variable_you_created=self。事实上,委托更好,因为(a)它是标准模式,并且(b)它避免了a和b相互依赖。“使用委托保持B的独立性。@michi和Brett Donald我认为你们应该看看这个:在许多情况下,块肯定是有价值的。我只是觉得在这种情况下他们不是正确的选择。