【问题标题】:Assign a value to a controller property in Swift在 Swift 中为控制器属性赋值
【发布时间】:2014-08-05 01:58:06
【问题描述】:

我试图在 Swift 中的视图之间传递一个 int 变量,但我不确定如何访问其他视图控制器的属性。

在 Objective C 中我会做这样的事情

UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
AnsViewController *ansViewController = [storyBoard instantiateViewControllerWithIdentifier:@"ansView"];
ansViewController.num = theNum;
[self presentViewController:ansViewController animated:YES completion:nil];

在另一个 viewcontroller.h 文件中,我会写这个来声明获取数据的属性

@property (nonatomic) int num;

现在对于 Swift 我有这个

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)

在另一个视图控制器的另一个 .swift 文件中,我声明了 num

let num: int

我很确定这不是正确的做法,因为我在这一行遇到错误

ansViewController.num = theNum;

它说,“UIViewController 没有一个名为 num 的成员”我将如何解决这个错误,我做错了什么?

谢谢

【问题讨论】:

  • let num: int 行在哪里?你能添加更多代码吗?
  • 我将 let num : int 行放在 AnsViewController 的 swift 文件中,就在所有 @IBOutlet var 行的下方

标签: ios objective-c uiviewcontroller swift ios8


【解决方案1】:

问题

在 Objective C 中,您已将 ansViewController 显式定义为 AnsViewController*,它具有属性 num。

但是,在您的 Swift 代码中,您已将 ansViewController 明确定义为 UIViewController,而不是 AnsViewController。因此,编译器不知道这实际上是 AnsViewController,还是其他一些 UIViewController 子类,或者只是一个普通的 UIViewController。

现在寻找解决方案。

我们将尝试将返回的值向下转换为 AnsViewController,然后在向下转换成功时访问该属性(我假设它总是如此,但与您的其余代码和 nib 无关,我不能确定)。

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... 的返回值是确定性的),那么您可以更简洁一点:

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)

【讨论】:

  • 谢谢!这似乎清除了所有错误,但创建了一个新错误。 ansViewController.num = theNum 所在的行给了我一个错误,说 CInt 不能转换为 Int。我还必须将我声明 num 的部分更改为 var num : Int = 0 以消除错误。我应该这样做吗?
  • 我认为我收到了这个错误,因为我必须使用 bridgeToObjectiveC().intValue 从文本字段中获取 int 值
  • 我不确定 theNum 来自哪里,但试试这个:ansViewController.num = Int(theNum)
  • 之所以有效是因为 Swift 在类型方面非常严格,并且不将 Ints 和 CInts 视为可互换的。您需要从 CInt 对象初始化一个新的 Int 对象。 Int(cInt: CInt) 本质上是被调用的,一个从 CInt 创建 Int 的 init 方法。
猜你喜欢
  • 1970-01-01
  • 2011-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-02
  • 1970-01-01
  • 2013-08-03
  • 2019-06-26
相关资源
最近更新 更多