如何使用 Swift 语言将数据从弹出的模型视图控制器传递到先前的视图控制器?
How to pass data from the popped model view controller to the previous view controller using Swift language?
我知道我们可以使用模型 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 中的块,例如:
// 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中得到了相同的城市值。
但是,我对这个街区感到困惑。我们怎样才能做到这一点来传递数据呢?从整个项目来看,街区似乎是一种隧道?然后我们可以从一侧传递数据,在另一侧获取数据。
之前是我对block的理解。这个对吗?
顺便说一句,如何在 swift 中使用块?非常感谢。
在协议中声明你的方法。
并使用委托调用您的方法。
要在视图控制器之间传递数据,您还可以使用 NSNotification。
例如:
在VC1中添加通知的观察者
- (void)viewDidLoad {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(rcvData:) name:@"passData" object:nil];
}
- (void)rcvData:(NSNotification*)notification {
NSLog(@"%@",notification.object);
}
Post 来自 VC2 的通知
NSString *name = @"xyz";
[[NSNotificationCenter defaultCenter] postNotificationName:@"passData" object:name userInfo:nil];
数据会在- rcvData:
方法中传递给VC1
我知道有 3 种方法可以将数据传回之前的 viewcontroller。
NSNotification - 当你在广播时。当呼叫必须在几个 class 中到达时使用它(注册通知的 class)。关系 - 一对多。建议您将代表设为弱类型。在你的情况下不推荐。
代表 - 协议投诉代表用于一对一class 沟通。它们 obj-c/swift 基于并且最适合任何数量的数据。它们没有 Blocks/Closures 快。关系——一对一。建议您制作块副本类型。
here is the example
Blocks/Closures - 积木速度很快。它们是用 C API 编写的包装器。尽管它们的语法是随意的。块像变量一样使用。它们包含一段你想在后期执行的代码(可能来自另一个 class)。块创建在其中编写的代码语句的副本,包括引用。
here is the example and here
我知道我们可以使用模型 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 中的块,例如:
// 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中得到了相同的城市值。
但是,我对这个街区感到困惑。我们怎样才能做到这一点来传递数据呢?从整个项目来看,街区似乎是一种隧道?然后我们可以从一侧传递数据,在另一侧获取数据。
之前是我对block的理解。这个对吗? 顺便说一句,如何在 swift 中使用块?非常感谢。
在协议中声明你的方法。 并使用委托调用您的方法。
要在视图控制器之间传递数据,您还可以使用 NSNotification。
例如: 在VC1中添加通知的观察者
- (void)viewDidLoad {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(rcvData:) name:@"passData" object:nil];
}
- (void)rcvData:(NSNotification*)notification {
NSLog(@"%@",notification.object);
}
Post 来自 VC2 的通知
NSString *name = @"xyz";
[[NSNotificationCenter defaultCenter] postNotificationName:@"passData" object:name userInfo:nil];
数据会在- rcvData:
方法中传递给VC1
我知道有 3 种方法可以将数据传回之前的 viewcontroller。
NSNotification - 当你在广播时。当呼叫必须在几个 class 中到达时使用它(注册通知的 class)。关系 - 一对多。建议您将代表设为弱类型。在你的情况下不推荐。
代表 - 协议投诉代表用于一对一class 沟通。它们 obj-c/swift 基于并且最适合任何数量的数据。它们没有 Blocks/Closures 快。关系——一对一。建议您制作块副本类型。 here is the example
Blocks/Closures - 积木速度很快。它们是用 C API 编写的包装器。尽管它们的语法是随意的。块像变量一样使用。它们包含一段你想在后期执行的代码(可能来自另一个 class)。块创建在其中编写的代码语句的副本,包括引用。 here is the example and here