使用块将数据传递回视图控制器 [英] Using blocks to pass data back to view controller

查看:78
本文介绍了使用块将数据传递回视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看此问题

其中一个答案显示了如何使用块向后传递数据查看 prepareForSegue 方法。我的理解是这种方法应该用于向前传递数据,而不是向后传递数据。

One of the answers shows how to use blocks to pass data backwards view the prepareForSegue method. My understanding is this method should really be used to pass data forward, not backwards.

我想为此目的尝试阻止 - 将数据传回另一个viewController。

I'm wanting to try blocks out for this purpose - passing data back to another viewController.

我的问题是:如何在不使用 prepareForSegue 方法的情况下执行此操作?
我可以在UITableView中使用 - didselectRowAtIndexPath 并关闭视图 - 但接收视图如何通知有数据返回,没有使用代表?

My question is: How do I do this, without using prepareForSegue method? I could use for instance in a UITableView - didselectRowAtIndexPath and dismiss the view - but then how does the receiving view get "notified" there is data to come back, without using a delegate?

推荐答案

向后发送数据


  1. secondViewController.h 文件

  1. declare block in your secondViewController.h file

@property(非原子,复制)void(^ myBlock)(NSString *);

调用块,无论您需要从.m文件传递数据 secondViewController

call block wherever you need to pass data from .m file of secondViewController

myBlock(@这将显示在firstViewController中);

3.在 firstViewController 中导入 .h 文件以上.m 文件
并将定义为

3.import above .h file in your firstViewController .m file and define your block as

secondViewController *ref =[[secondViewController alloc ]init];

     ref.myBlock =^void(NSString *data)
    {
    self.labelOffirstviewcontroller=data;
    };

这篇关于使用块将数据传递回视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆