可选解包失败,当插座连接正确时
Optional unwrap fail, when outlets connected properly
我正在开发简单的应用程序,用户可以在其中登录并阅读来自网络的文章。我最近添加了一段代码,在处理转场时在第二个视图中设置标题,但不幸的是我在第二页的标题为零。我在故事板中有一个 object 正确连接到视图控制器中的变量,我检查了两次。我不知道该怎么办,也许我没有正确打开包装。
代码:
import UIKit
class MainViewController: UIViewController, UITabBarDelegate, UITabBarControllerDelegate, UIToolbarDelegate {
@IBOutlet var titleLabel: UILabel!
@IBOutlet var newsBar: UIToolbar!
@IBOutlet var accountBar: UITabBar!
override func viewDidLoad() {
super.viewDidLoad()
accountBar.delegate = self
newsBar.delegate = self
titleLabel.backgroundColor = UIColor(netHex: 0x00B0E4)
titleLabel.textColor = UIColor.whiteColor()
titleLabel.font = UIFont.boldSystemFontOfSize(16.0)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func shouldPerformSegueWithIdentifier(identifier: String, sender: AnyObject?) -> Bool {
return true
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let next: SectionViewController = segue.destinationViewController as! SectionViewController
switch segue.identifier! {
case "hardwareSectionSegue":
next.titleLabel.text = "Rubrika o hardwaru"
case "softwareSectionSegue":
next.titleLabel.text = "Rubrika o softwaru"
case "webSectionSegue":
next.titleLabel.text = "Rubrika o webových technologiích"
case "programmerSectionSegue":
next.titleLabel.text = "Rubrika o programování"
case "mobileSectionSegue":
next.titleLabel.text = "Rubrika o mobilních zažízeních"
default:
next.titleLabel.text = "Rubrika nenalezena"
next.titleLabel.backgroundColor = UIColor.redColor()
}
}
@IBAction func unwindSegue(unwindSegue: UIStoryboardSegue) {}
}
异常发生在我试图为 next.titleLabel.text
赋值的第一种情况下。它说 titleLabel
是零,它应该不是。
SectionViewController:
import UIKit
class SectionViewController: UIViewController {
@IBOutlet var titleLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
我很确定这是由类型转换引起的,但是如果 segue 甚至不知道下一个视图的类型是什么,我该如何正确设置属性?
我知道你想做什么,但恐怕 iOS 不允许你那样做。在您的 prepareForSegue()
方法中,您试图修改通过直接设置文本值创建的视图控制器。
iOS 尽可能多地延迟加载,这意味着此时在您的程序中实际上尚未创建标签 – 它确实是 nil
。如果你在该行上放置一个断点,你应该能够在调试器中 运行 po next.titleLabel
window 看到 nil
回来。如果您首先 运行 po next.view
它将强制 iOS 创建视图及其所有子视图,此时 运行ning po next.titleLabel
将再次按您的预期工作.
如果您尝试在 Xcode 中创建模板主从应用程序项目,您将看到正确的解决方案:在新视图控制器中设置一个 属性,然后将该值传输到您的标签在 viewDidLoad()
.
总结:当您从视图控制器 A 导航到视图控制器 B 时,不要让 A 尝试配置 B 的 UI。相反,向 B 发送它需要的值,并让 B 自行配置。
我正在开发简单的应用程序,用户可以在其中登录并阅读来自网络的文章。我最近添加了一段代码,在处理转场时在第二个视图中设置标题,但不幸的是我在第二页的标题为零。我在故事板中有一个 object 正确连接到视图控制器中的变量,我检查了两次。我不知道该怎么办,也许我没有正确打开包装。
代码:
import UIKit
class MainViewController: UIViewController, UITabBarDelegate, UITabBarControllerDelegate, UIToolbarDelegate {
@IBOutlet var titleLabel: UILabel!
@IBOutlet var newsBar: UIToolbar!
@IBOutlet var accountBar: UITabBar!
override func viewDidLoad() {
super.viewDidLoad()
accountBar.delegate = self
newsBar.delegate = self
titleLabel.backgroundColor = UIColor(netHex: 0x00B0E4)
titleLabel.textColor = UIColor.whiteColor()
titleLabel.font = UIFont.boldSystemFontOfSize(16.0)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func shouldPerformSegueWithIdentifier(identifier: String, sender: AnyObject?) -> Bool {
return true
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let next: SectionViewController = segue.destinationViewController as! SectionViewController
switch segue.identifier! {
case "hardwareSectionSegue":
next.titleLabel.text = "Rubrika o hardwaru"
case "softwareSectionSegue":
next.titleLabel.text = "Rubrika o softwaru"
case "webSectionSegue":
next.titleLabel.text = "Rubrika o webových technologiích"
case "programmerSectionSegue":
next.titleLabel.text = "Rubrika o programování"
case "mobileSectionSegue":
next.titleLabel.text = "Rubrika o mobilních zažízeních"
default:
next.titleLabel.text = "Rubrika nenalezena"
next.titleLabel.backgroundColor = UIColor.redColor()
}
}
@IBAction func unwindSegue(unwindSegue: UIStoryboardSegue) {}
}
异常发生在我试图为 next.titleLabel.text
赋值的第一种情况下。它说 titleLabel
是零,它应该不是。
SectionViewController:
import UIKit
class SectionViewController: UIViewController {
@IBOutlet var titleLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
我很确定这是由类型转换引起的,但是如果 segue 甚至不知道下一个视图的类型是什么,我该如何正确设置属性?
我知道你想做什么,但恐怕 iOS 不允许你那样做。在您的 prepareForSegue()
方法中,您试图修改通过直接设置文本值创建的视图控制器。
iOS 尽可能多地延迟加载,这意味着此时在您的程序中实际上尚未创建标签 – 它确实是 nil
。如果你在该行上放置一个断点,你应该能够在调试器中 运行 po next.titleLabel
window 看到 nil
回来。如果您首先 运行 po next.view
它将强制 iOS 创建视图及其所有子视图,此时 运行ning po next.titleLabel
将再次按您的预期工作.
如果您尝试在 Xcode 中创建模板主从应用程序项目,您将看到正确的解决方案:在新视图控制器中设置一个 属性,然后将该值传输到您的标签在 viewDidLoad()
.
总结:当您从视图控制器 A 导航到视图控制器 B 时,不要让 A 尝试配置 B 的 UI。相反,向 B 发送它需要的值,并让 B 自行配置。