在Swift中为控制器属性赋值 [英] Assign a value to a controller property in Swift
问题描述
我试图在Swift中的视图之间传递一个int变量,但我不确定如何访问其他View控制器的属性。
I am trying to pass an int variable between views in Swift but I'm not sure how to access the other View controller's property.
在Objective CI中会做类似这样的事情
In Objective C I would do something like this
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
AnsViewController *ansViewController = [storyBoard instantiateViewControllerWithIdentifier:@"ansView"];
ansViewController.num = theNum;
[self presentViewController:ansViewController animated:YES completion:nil];
在另一个viewcontroller.h文件中我会写这个来声明属性来获取数据
And in the other viewcontroller.h file I would write this to declare the property to get the data
@property (nonatomic) int num;
现在对于Swift我有这个
Now for Swift I have this
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let ansViewController : UIViewController = storyBoard.instantiateViewControllerWithIdentifier("ansView") as UIViewController
ansViewController.num = theNum;
self.presentViewController(ansViewController, animated:true, completion:nil)
and the其他视图控制器的其他.swift文件我通过执行来声明num
and in the other .swift file for the other view controller I declared num by doing
let num: int
我很确定这不是正确的方法,因为我在这一行收到错误
I'm pretty sure that isn't the right way to do it because I get an error on this line
ansViewController.num = theNum;
它说,UIViewController没有名为num的成员我将如何解决此错误,我做错了什么?
and it says, "UIViewController does not have a member named num" How would I resolve this error and what have I done wrong?
谢谢
推荐答案
问题
在Objective C中,你已经明确地将ansViewController定义为AnsViewController *,它具有属性num。
In Objective C, you've explicitly defined ansViewController as an AnsViewController*, which has the property num.
但是,在你的Swift代码中,你已经明确地将ansViewController定义为UIViewController,而不是AnsViewController。因此,编译器不知道这实际上是一个AnsViewController,还是其他一些UIViewController子类,或者只是一个vanilla UIViewController。
In your Swift code, though, you've explicitly defined ansViewController as a UIViewController, not an AnsViewController. So, the compiler has no idea if this is actually an AnsViewController, or some other UIViewController subclass, or just a vanilla UIViewController.
现在解决方案。
我们将尝试将返回的值转发为AnsViewController,然后如果downcast成功则访问该属性(我假设它总是会,但是从你的其余代码和笔尖的上下文中,我不能确定。)
We're going to try to downcast the returned value as an AnsViewController, then access that property if the downcast succeeds (I'm assuming it always will, but out-of-context from the rest of your code and nibs, I can't be sure).
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
// To be safe, let's attempt to downcast the returned value as an AnsViewController
if let ansViewController = storyBoard.instantiateViewControllerWithIdentifier("ansView") as? AnsViewController {
// We get here if the "as?" succeeds with a non-nil value
ansViewController.num = theNum;
self.presentViewController(ansViewController, animated:true, completion:nil)
} else {
// Out of context, I can't see why this case would (or could) ever happen
}
现在,如果你知道这将永远成功(从我所看到的, -instantiateWith的返回值是确定性的,然后你可以更简洁一点:
Now, if you know this will always succeed (from what I can see, the return value of -instantiateWith... is deterministic), then you can be a bit more concise:
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
// Force the downcast as an AnsViewController (this could crash at runtime
// if the return value is nil or not an AnsViewController, so again,
// the previous example is safer
let ansViewController = storyBoard.instantiateViewControllerWithIdentifier("ansView") as AnsViewController
ansViewController.num = theNum;
self.presentViewController(ansViewController, animated:true, completion:nil)
这篇关于在Swift中为控制器属性赋值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!