【发布时间】:2016-09-13 05:31:22
【问题描述】:
如何将数据传递到我以编程方式呈现但已在 IB 中创建的视图控制器中?现在我有代码可以在用户单击按钮时拉出视图,但不清楚如何将数据发送到该视图中。
我正在尝试使用下面的代码,但被告知“UIViewController 类型的值没有成员“数据””
@IBAction func showPossButton(sender: UIButton) {
print("Show data table.")
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("PossTable")
var data:Data!
vc.data = data
self.presentViewController(vc, animated: true, completion: nil)
}
我在这里做错了什么?
【问题讨论】:
-
let vc = storyboard.instantiateViewControllerWithIdentifier("PossTable") as! PossibleViewController(或该场景的任何基类)。 -
这似乎奏效了。你能解释一下我为什么不知道这样做或那在做什么而缺少的概念吗?是不是我需要同时指向 IB 中的场景和 xCode 的视图控制器才能将我的数据对象与正确的东西相关联?而且我只是指向场景而不是视图控制器本身,我试图在其中获取数据!对象?
-
概念如下:
instantiateViewControllerWithIdentifier方法被定义为返回一个UIViewController。编译器无法知道您碰巧在 Interface Builder 中定义的UIViewController子类,因此您必须告诉它。所以,是的,你必须告诉 IB 在实例化场景时使用什么基类,你还必须告诉编译器将此变量转换为什么。 -
好的。我认为我需要更好地理解一般的“铸造”,因为这些 UIViewControllers 都只是变量,尽管是花哨的变量,但对我来说还不直观。
标签: ios swift presentviewcontroller