我正在尝试在另一个 View Controller 上设置 UILabel 的值。但是,当我尝试为其他 View Controller 的 UILabel 等设置临时值时,我一直得到一个 nil 值。这是执行 segue 的函数:
func swipe(Sender: UISwipeGestureRecognizer!) {
print("swiped left")
let vc = self.storyboard!.instantiateViewControllerWithIdentifier("OnDeck") as! OnDeck
vc.firstBase = UIView(frame: CGRectMake(178, 136, 10, 10))
vc.firstBase.backgroundColor = UIColor.redColor()
vc.firstBase.layer.cornerRadius = vc.firstBase.frame.size.width/2
self.presentViewController(vc, animated: true, completion: nil)
}
我在行 vc.firstBase.backgroundColor = UIColor.redColor()
fatal error: unexpectedly found nil while unwrapping an Optional value
在尝试声明 socket 的温度值时,我是否遗漏了什么?
最佳答案
您不应设置那些相关的 UI 元素,因为 UI 尚未加载。相反,传递一个值并将其设置在目标 Controller 的 viewDidLoad
class OnDeck: UIViewController {
var color = UIColor.white()
override func viewDidLoad() {
...
firstBase = color
}
}
然后在您的swipe
方法中,只传递值而不是修改卸载的UILabel
或UIView
。否则,将 UI 相关代码移动到目标 viewDidLoad()
,例如 cornerRadius、frame size 等
关于swift - 一个 VC 对另一个 VC 的错误设置值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44485525/