2014-09-30 24 views
1

可选的价值我想这个代码意外地发现零而展开的迅速

let secondView = SecondViewController(nibName: "secondView", bundle: nil) 
    secondView.secondViewLabel.text = self.TextBox.text 
    navigationController?.pushViewController(secondView, animated: true) 

我试图从文本框中的值传递给一个标签,在未来视图controller.But我得到一个错误

我也试着将文本框的值复制到第二个视图控制器中声明的sting中,该控制器也返回nil。

let secondView = SecondViewController(nibName: "secondView", bundle: nil) 
    secondView.toPass = self.TextBox.text 
    navigationController?.pushViewController(secondView, animated: true) 
    println(secondView.toPass) // i get the string printed here 

在第二视图控制器

 var toPass:String! 

鉴于没有负载

secondLabel.text = toPass 
    println(toPass) // i get the string printed nil 

帮助我,大家好我是新来的迅速

+0

什么是TextBox?它是什么类型?什么是TextBox.text的类型? – JohnVanDijk 2014-09-30 06:17:31

+0

@IBOutlet weak var TextBox:UITextField! – vishnuvarthan 2014-09-30 06:32:05

回答

0

让toPass一个String有条件的类型一样所以

var toPass: String?

在viewDidLoad中,做到这一点:

if let passedString = toPass { 
    secondLabel.text = toPass 
    println(toPass) 
} 

然后把一个断点,如果让块内,看是否toPass过的值或者没有被设置的。真的需要知道TextBox是什么类型。

[编辑2]

如果问题该值被设置的viewController secondView已经从笔尖解码之前,我建议你做一个使用initWithNib便利初始化,但也需要在额外的标题字符串或任何它。在那里,您可以将textLabel文本的值设置为新初始化程序所采用的参数。当然这意味着你必须继承UIViewController。

+0

它没有进入循环。我已经提到toPass的值为空 – vishnuvarthan 2014-09-30 06:37:37

+0

为什么我不能直接传递字符串,如我在第一个问题中提到的。任何想法? – vishnuvarthan 2014-09-30 06:39:10

+0

IMO'secondView.secondViewLabel.text = self.TextBox.text'在nib解码之前被调用,因此它没有被设置,因为'secondViewLabel'还没有绑定。 – JohnVanDijk 2014-09-30 07:04:09

相关问题