【问题标题】:Koloda delegate : fatal error: unexpectedly found nil while unwrapping an Optional valueKoloda 委托:致命错误:在展开可选值时意外发现 nil
【发布时间】:2015-08-31 01:26:48
【问题描述】:

我在我的项目中使用 Koloda,每当我特别尝试加载一个视图控制器时,我都会在 kolodaView.dataSource = self 上收到错误“致命错误:在展开可选值时意外发现 nil”,然后如果它被注释掉我在kolodaView.delegate = self 上遇到同样的错误。我对 IOS 和 Swift 还很陌生,所以我不确定发生了什么。以下是 Koloda viewcontroller 中的代码显示错误的地方:

 override func viewDidLoad() {
    super.viewDidLoad()

    kolodaView.dataSource = self
    kolodaView.delegate = self

    self.modalTransitionStyle = UIModalTransitionStyle.FlipHorizontal
}

以下是我尝试加载的UIViewController 中的代码。它们也没有通过任何 segues 链接。

import Foundation
import UIKit

class genderSelection1: ViewController, UITableViewDelegate, UITableViewDataSource{




@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.delegate = self
    self.tableView.dataSource = self

}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// MARK: - Table view data source

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 2
}

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

    let  headerCell = tableView.dequeueReusableCellWithIdentifier("headerCell") as! UITableViewCell
    headerCell.backgroundColor = UIColor.groupTableViewBackgroundColor()
    return headerCell
}

func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {

    return 1
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return 1
}


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell

    return cell
}

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

    tableView.deselectRowAtIndexPath(indexPath, animated: true)

}
}

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    kolodaView 实例为零。通过将 NSLog 放在 super.viewDidLoad() 之后来验证这一点。

    NSLog("kolodaView  \(kolodaView)");
    

    我认为,您没有正确连接您的 IBOutlet。你需要检查一下。

    【讨论】:

    • 我重新连接了IBOutlet,但仍然出现 nil 错误。还值得注意的是,当我尝试加载 koloda viewController 时,一切正常。只是当我遇到问题时加载另一个view controller
    • 你可能想参考这个问题:stackoverflow.com/questions/40607951/…
    猜你喜欢
    • 1970-01-01
    • 2016-01-26
    • 2016-02-29
    • 2020-09-19
    • 2016-01-08
    相关资源
    最近更新 更多