[英]handle detail page for table view controller in swift
I have a table view controller with cells. 我有一个带有单元格的表视图控制器。 on clicking a cell I load another view controller and I want to handle elements on this view controller.
单击单元格时,我加载了另一个视图控制器,并且我想处理该视图控制器上的元素。 on the detail view controller I placed a label and in the first step I want to set the text of the label, but I get an exception
fatal error: unexpectedly found nil while unwrapping an Optional value
and I don't know why. 在详细信息视图控制器上,我放置了一个标签,并且第一步要设置标签的文本,但出现一个
fatal error: unexpectedly found nil while unwrapping an Optional value
的异常fatal error: unexpectedly found nil while unwrapping an Optional value
,我不知道为什么。 Are there any solutions? 有什么解决办法吗?
This is the part of my table view controller on clicking a cell: 这是我的表格视图控制器中单击单元格的一部分:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let vcMissionDetail : ViewControllerMissionDetail = self.storyboard?.instantiateViewControllerWithIdentifier("MissionDetail") as ViewControllerMissionDetail;
//load detail view controller
self.presentViewController(vcMissionDetail, animated: true, completion: nil)
//set label text
//at this line I get the exception -> label is nil
vcMissionDetail.label.text = "Test"
}
And this is my detail view controller (very simple): 这是我的详细信息视图控制器(非常简单):
import UIKit
class ViewControllerMissionDetail: UIViewController {
@IBOutlet var label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
THX! 谢谢!
I think the solution to your problem is pretty straight forward. 我认为解决您的问题非常简单。 If I'm understanding correctly, you want to change the label on your detail view controller when you segue from table view controller using the didSelectRowAtIndexPath.
如果我理解正确,那么当您使用didSelectRowAtIndexPath从表视图控制器中选择时,您想更改详细视图控制器上的标签。 The place where you are probably making a mistake is, you're trying to change the label in your detail view controller from the table view controller.
您可能会犯一个错误的地方是,您正在尝试从表视图控制器更改详细信息视图控制器中的标签。 The correct approach to your problem would be to set the label text in the 'viewDidLoad' or 'viewDidAppear' method of the detail view controller.
解决问题的正确方法是在详细视图控制器的“ viewDidLoad”或“ viewDidAppear”方法中设置标签文本。
override func viewDidLoad() {
super.viewDidLoad()
//set label text here
}
override func viewDidAppear() {
super.viewDidLoad()
//OR set label text here
}
instantiateViewControllerWithIdentifier
returns an optional because it may fail due to several reasons. instantiateViewControllerWithIdentifier
返回一个可选参数,因为它可能由于多种原因而失败。 So you should better unwrap it conditionally: 因此,您最好有条件地打开它:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if let vcMissionDetail : ViewControllerMissionDetail = self.storyboard?.instantiateViewControllerWithIdentifier("MissionDetail") as ViewControllerMissionDetail {
//set label text before presenting the viewController
vcMissionDetail.label.text = "Test"
//load detail view controller
self.presentViewController(vcMissionDetail, animated: true, completion: nil)
}
}
Further things to double check: 要进一步检查的其他事项:
Is ViewControllerMissionDetail
set as the custom class for your viewController in the identity inspector in InterfaceBuilder? 是否在InterfaceBuilder的身份检查器中将
ViewControllerMissionDetail
设置为viewController的自定义类?
If the vcMissionDetail
is successfully instantiated and it still crashes then delete the label's outlet connection in InterfaceBuilder and recreate it. 如果成功实例化了
vcMissionDetail
,但它仍然崩溃,请在InterfaceBuilder中删除标签的插座连接并重新创建它。
For swift: 快速:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.performSegueWithIdentifier("showItemDetail", sender: tableView)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if segue.identifier == "showItemDetail" {
let indexPath:NSIndexPath = self.tableView.indexPathForSelectedRow()!
let detailVC:ItemDetailViewController = segue.destinationViewController as ItemDetailViewController
detailVC.item = items[indexPath.row] as Item
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.