Error: Found nil while unwrapping an Optional value; While Pass Data to the New Controller
Error: Found nil while unwrapping an Optional value; While Pass Data to the New Controller
我正在学习swift 2. 我正在尝试将点击的单元格 (UITableView) 的数据传递给新的视图控制器。但是我经常收到错误:代码行“destination!.label.text = valueToPass
”的“unexpectedly found nil while unwrapping an Optional value
”。
下面是我的代码。
我可以知道我应该怎么做才能解决它吗?我已经花了几个小时,但仍然坚持下去。
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let indexPath = tableView.indexPathForSelectedRow;
let Mycell = tableView.cellForRowAtIndexPath(indexPath!) as UITableViewCell!;
valueToPass = (Mycell.textLabel?.text)!
performSegueWithIdentifier("webNext", sender: self)
print("\(indexPath!.row)")
print(valueToPass)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "webNext") {
let destination = segue.destinationViewController as? web___ViewController
destination!.label.text = valueToPass
}
}
通过这样做:destination!.label.text = valueToPass
,您甚至在绘制或显示标签之前就已经为标签设置了一些文本。
而是使用一些变量将数据传递到下一个视图并显示 viewDidLoad
中的数据。
let destination = segue.destinationViewController as? web___ViewController
destination!.strValue = valueToPass
Web_VC:
var strValue:String?
func viewDidLoad(){
super.viewDidLoad()
label.text = strValue!
}
我正在学习swift 2. 我正在尝试将点击的单元格 (UITableView) 的数据传递给新的视图控制器。但是我经常收到错误:代码行“destination!.label.text = valueToPass
”的“unexpectedly found nil while unwrapping an Optional value
”。
下面是我的代码。
我可以知道我应该怎么做才能解决它吗?我已经花了几个小时,但仍然坚持下去。
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let indexPath = tableView.indexPathForSelectedRow;
let Mycell = tableView.cellForRowAtIndexPath(indexPath!) as UITableViewCell!;
valueToPass = (Mycell.textLabel?.text)!
performSegueWithIdentifier("webNext", sender: self)
print("\(indexPath!.row)")
print(valueToPass)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "webNext") {
let destination = segue.destinationViewController as? web___ViewController
destination!.label.text = valueToPass
}
}
通过这样做:destination!.label.text = valueToPass
,您甚至在绘制或显示标签之前就已经为标签设置了一些文本。
而是使用一些变量将数据传递到下一个视图并显示 viewDidLoad
中的数据。
let destination = segue.destinationViewController as? web___ViewController
destination!.strValue = valueToPass
Web_VC:
var strValue:String?
func viewDidLoad(){
super.viewDidLoad()
label.text = strValue!
}