我正在开发简单的应用程序,用户可以在其中登录并阅读网络上的文章。我最近添加了一个代码,它在处理 segue 时在第二个视图中设置标题,但不幸的是我在第二页的标题是 nil。我在情节提要中有一个对象正确连接到视图控制器中的变量,我检查了两次。我不知道该怎么做,也许我没有正确解开一些东西。
代码:
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 甚至不知道下一个视图将具有什么类型,我该如何正确设置属性?