【问题标题】:iOS Unexpectedly found nil while unwrapping an Optional ValueiOS 在展开可选值时意外发现 nil
【发布时间】:2016-01-15 17:19:30
【问题描述】:

我的应用程序将允许用户创建一个注册表单,它将使用 UITableView 显示。我在更新 UITableView 时遇到问题。只是为了解释一下发生了什么,我有两个场景。第一个场景显示创建的问题,第二个场景允许用户创建一个问题。第 1 个场景转至第 2 个场景,然后第 2 个场景展开转至使用问题数据看到的第 1 个场景。这是我的代码。

第一个场景:

@IBOutlet var tableViewObject: UITableView!
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

// code for creating tableView
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.questionsArray.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
    cell.textLabel?.text = self.questionsArray[indexPath.row].Label
    return cell
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

}
@IBAction func segueToView(sender: AnyObject) {

}

@IBAction func cancelToSecondViewController(segue:UIStoryboardSegue) {
}

@IBAction func saveQuestion(segue:UIStoryboardSegue) {
    if let CreateQuestion = segue.sourceViewController as? createQuestion {

        if (CreateQuestion.flag == 0) {
           let textinput = CreateQuestion.newInputQuestion
            questionsArray.append(textinput)
        }

        else {
            let multichoice = CreateQuestion.newMultiQuestion
            questionsArray.append(multichoice)
        }

        let indexPath = NSIndexPath(forRow: questionsArray.count-1, inSection: 0)
        tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
    }
}

第二幕:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "SaveQuestion" {
        if (questionType.selectedSegmentIndex == 0) {
            newInputQuestion = textInput(placeHolder: inputQuestionHint.text!, Label: inputQuestionTitle.text!, required: required.selectedSegmentIndex)
            flag = 0
        }

        else {
            var arrayOfAnswers = [String]()

            for (var i = 0; i < numAnswers.selectedSegmentIndex + 1; i++) {
                arrayOfAnswers.append(numAnswersArray[i].text!)
            }

            newMultiQuestion = multiChoice(answers: arrayOfAnswers, Label: multiQuestionTitle.text!, required: required.selectedSegmentIndex)
            flag = 1
        }
    }
}

我在尝试创建问题时收到以下错误:

致命错误:在展开可选值时意外发现 nil

它突出显示以下行:

tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)

【问题讨论】:

  • 实际上,我通过其他测试方式发现将其声明为类型 Question 是可行的。这不是与更新数组有关的错误的原因,因此我可能会编辑此问题。
  • tableView 声明在哪里?
  • 在第一个场景中。我将使用 tableview 代码编辑我的帖子
  • 您发布了 tableview 委托函数,但没有发布 tableView 声明的位置。是否有一行看起来像这样:@IBOutlet weak var tableView: UITableView!
  • 代码的第一行就是这么说的!

标签: ios arrays swift uitableview


【解决方案1】:

tableViewnil。由于它是由界面生成器配置的@IBOutlet,因此您在界面生成器中的配置方式有问题。

最可能的罪魁祸首是您没有在身份检查器中为视图控制器设置类。

转到界面生成器并确保实用程序窗格已打开。在实用程序窗格中打开身份检查器。靠近顶部是供您输入自定义视图控制器的类名的地方。因此,如果您的视图控制器名为 MyViewController,请确保在该字段中输入 MyViewController

如果这不起作用,则 @IBOutlet 配置不正确。检查您的连接,然后仔细检查它们。

【讨论】:

  • 所以我得到了它的工作,老实说我不确定我做了什么,但你的 cmets 把我引向了正确的方向。感谢您的帮助。
猜你喜欢
  • 2016-06-26
  • 2016-03-12
  • 1970-01-01
  • 1970-01-01
  • 2017-01-23
  • 2016-10-18
  • 2017-04-09
  • 2018-09-22
相关资源
最近更新 更多