Swift不允许我设置可变数据

时间:2016-04-08 07:10:05

标签: ios swift view controller segue

我正在尝试创建一个简单的联系人列表应用程序作为学习Swift的途径。不幸的是,无论我尝试什么,我都无法与我联系。详细信息显示在tableview的不同视图控制器上的可编辑文本字段中(包含主要联系人)。

以下是应该影响文本字段的代码块(我不确定要提供的其他代码)

 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "showDetail" {
        if let indexPath = self.tableView.indexPathForSelectedRow {
            let object = objects[indexPath.row]
            let controller = (segue.destinationViewController as! UINavigationController).topViewController as! DetailViewController
            print(object.firstName)
            controller.fiName?.text = object.firstName
            controller.laName?.text = object.lastName
            controller.miName?.text = object.middleName
            controller.aess?.text = object.address
            controller.phoNumber?.text = object.phoneNumber
            controller.yearBirth?.text = String(object.age)
            print(controller.fiName)
            controller.detailItem = object
            controller.navigationItem.leftBarButtonItem = self.splitViewController?.displayModeButtonItem()
            controller.navigationItem.leftItemsSupplementBackButton = true
        }
    }

第一条打印行的结果与我预期的一样,即联系人的姓名,但程序在第二次打印时崩溃,并且出现了' EXC_BAD_INSTRUCTION' /致命错误:在解包和可选值时意外发现nil。

为什么我不能设置controller.fiName?.text?提前感谢您的帮助。

3 个答案:

答案 0 :(得分:2)

您过早地设置了fiName标签文本,标签尚未分配。而是通过对象,然后在下一个控制器的viewDidLoad中设置文本。

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "showDetail" {
        if let indexPath = self.tableView.indexPathForSelectedRow {
        let object = objects[indexPath.row]
        let controller = (segue.destinationViewController as! UINavigationController).topViewController as! DetailViewController

        //pass through object:
        controller.object = object

        print(object)
    }
}

然后在下一个viewController viewDidLoad中设置文本:

override viewDidLoad() {
    //Now set the text value here:
    fiName?.text = object.firstName
}

答案 1 :(得分:2)

我猜你的filNameUILabel还是UITextField权利?

如果您在DetailViewController中查看代码,您会注意到这一行:

@IBOutlet weak var filName: UITextField!

您会在行尾看到!吗?

这意味着,变量(aka outlet)filName不能在初始化时设置,但稍后会设置。

现在,您所做的就是初始化您的类,并尝试访问尚未在初始化时设置的属性。

要解决此问题,请在DetailViewController中创建一个对象实例,并将该实例设置为所选对象,然后在viewDidLoad函数中设置标签属性。

这样的事情:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "showDetail" {
        if let indexPath = self.tableView.indexPathForSelectedRow {
            let object = objects[indexPath.row]
            let controller = (segue.destinationViewController as! UINavigationController).topViewController as! DetailViewController
            controller.object = object
        }
}

然后在viewDidLoad的{​​{1}}中:

DetailViewController

答案 2 :(得分:1)

只需将对象传递到prepareForSegue

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "showDetail" {
            if let indexPath = self.tableView.indexPathForSelectedRow {
                let object = objects[indexPath.row]
                let controller = (segue.destinationViewController as! UINavigationController).topViewController as! DetailViewController
                controller.detailItem = object
                controller.navigationItem.leftBarButtonItem = self.splitViewController?.displayModeButtonItem()
                controller.navigationItem.leftItemsSupplementBackButton = true
            }
        }
    }

然后应在viewDidLoad

中设置插座
class DetailViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        fiName?.text = detailItem.firstName
        laName?.text = detailItem.lastName
        miName?.text = detailItem.middleName
        aess?.text = detailItem.address
        phoNumber?.text = detailItem.phoneNumber
        yearBirth?.text = String(detailItem.age)
    }
}