我有以下两个控制器: 1)
class DataViewController: UIViewController {
@IBOutlet weak var dataLabel: UILabel!
var dataObject: String = ""
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.dataLabel!.text = dataObject
}
}
和2)
class HubListController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet var tableView: UITableView!
let textCellIdentifier = "TextCell"
var token: String = ""
var userId: Int = 0
var work: Array<AnyObject> = ["1", "2", "3"]
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print("getting count: \(self.work.count)")
return self.work.count
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell!
cell.textLabel?.text = "Foo"
return cell
}
// MARK: UITableViewDelegate Methods
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
}
}
然后我在第一个视图上有一个按钮,它被分隔到第二个视图,不可避免地导致崩溃
fatal error: unexpectedly found nil while unwrapping an Optional value
使用调试器进行检查,结果发现tableView
为零。为什么?我怎样才能让这个(微不足道的)示例起作用?