我想将属性传递给从 Storyboard 中的容器 View 创建的 View Controller 。问题是我不知道我该怎么做。
蓝色矩形是我有两个容器 View 的区域,每个容器都引用一个 View Controller (一个是顶部的自定义 TableView Controller ,我感兴趣,另一个是下面的 View Controller )。
问题是我的自定义 TableView Controller 需要一些属性。我想从我实例化 Storyboard 的类中传递属性( Storyboard是从一个 toher 类调用的)。包含容器 View 的 View Controller 实例化如下所示:
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Phenotype" bundle:nil];
GeneralViewController *vc = [sb instantiateViewControllerWithIdentifier:@"phenotype"];
[vc setProperty:property]; // I would like to pass this property to the custom table view controller.
[self.navigationController pushViewController:vc animated:YES];
此时我尝试从实例化 stroyboard 的类中获取 TableView Controller 以设置属性
TableViewController *tablevc = [sb instantiateViewControllerWithIdentifier:@"table"];
[tablevc setProperty:property];
...但没有任何成功。
谁知道如何直接从实例化 Storyboard的 View Controller 访问自定义 TableView Controller 的属性?
谢谢你的帮助,
最佳答案
选择从蓝色矩形到顶部 tableView 的嵌入转场,并给它一个转场标识符 :) 假设转场标识符是“abcd”。
在你的 ViewController 中有容器写入
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "abcd" {
let tableView : YourTableViewClass = segue.destination as! YourTableViewClass
//pass whatever params want :)
}
}
编辑
嵌入式 segues 的行为就像其他 segues 一样,除了一个异常(exception),其他 segues 你必须以编程方式执行它们或触发一些事件,因为嵌入式 segues 在容器加载时自动执行 :)
所以你可以有 segue 标识符,在 prepareForSegue 中编写代码,就像你为其他 segue 做的那样:)
关于ios - Xcode - 将属性传递给 Storyboard 中的 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42785003/