【发布时间】:2015-06-09 06:33:12
【问题描述】:
我知道我们可以使用模型 segue presentViewController 切换到 VC1 到 VC2。 我们可以在prepareForSegue方法中使用segue将数据从VC1传递到VC2,如下所示:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var vc2: VC2ViewController = segue.destinationViewController as! VC2ViewController
vc2.desiredData = "The data wanted by VC2"
}
但是,当使用dismissViewContoller 时,如何将数据从VC2 重新传递回VC1? 我知道我们可以使用objective-C中的block,例如:
// VC1.h file:
typedef void(^SelectedCity)(NSString *);
@property (copy, nonatomic) SelectedCity selectedCity;
// VC1.m file:
__weak VC1ViewController *weakVC1 = self;
_selectedCity = ^(NSString *city) {
weakVC1.cityLabel.text = "city";
}
// VC2.h file:
typedef void(^SelectedCity)(NSString *);
@property (copy, nonatomic) SelectedCity selectedCity;
// VC2.m file:
if (_selectedCity) {
_selectedCity(cell.textLabel.text); //get the city from tableView cell
}
情况是:我从VC2的tableView单元格中选择了城市,然后在VC1中得到了相同的城市值。
但是,我对这个街区感到困惑。我们如何做到这一点来传递数据?在整个项目中,街区似乎是一种隧道吗?然后我们可以从一侧传递数据,从另一侧获取数据。
之前是我对区块的理解。它是否正确? 顺便说一句,如何快速使用块?非常感谢。
【问题讨论】:
-
是的,NSNotification 是在 viewController 之间传递数据的好方法。但我仍然想知道该块是如何工作的。谢谢大家。
标签: ios objective-c block