将容器视图添加到代码时会发生致命错误

时间:2017-10-26 19:51:05

标签: swift xcode optional fatal-error uicontainerview

我意识到有关致命错误的类似问题,并且已经询问了解包选项,但在这种情况下,我添加到代码中的唯一内容是两个出口(我已检查过他们的连接)现在我遇到了错误之前没有引发错误的地方。

我的代码运行良好,然后我添加了两个容器视图,它崩溃说:"致命错误:在解开一个Optional值时意外发现nil,"但我无法弄清楚可选项的位置。它说它与名为segmentBackground的UIview有关,但在添加容器视图之前没有引发任何错误。

感谢您的高级帮助。

这是我的代码:

class DetailMosaicViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

// Properties
var mosaic: String?
var overviewBottomBorder: UIView?
var modelsBottomBorder: UIView?
var sectionTitles = [String]() {
    didSet {
        profileTableView.reloadData()
    }
}
var mainText = [String]() {
    didSet {
        profileTableView.reloadData()
    }
}

// Outlets
@IBOutlet weak var segmentBackground: UIView!
@IBOutlet weak var profileTableView: UITableView!
@IBOutlet weak var profileModelsSegment: UISegmentedControl!
@IBOutlet weak var modelsContainerView: UIView!
@IBOutlet weak var profileContainerView: UIView!



override func viewDidLoad() {
    super.viewDidLoad()

    if let mosaic = mosaic {
        navigationItem.title = mosaic
    }
    self.navigationController?.navigationBar.tintColor = UIColor(red:0.00, green:0.87, blue:0.39, alpha:1.0)
    configureSegmentController()
    configureText()
    // Configures TableView
    profileTableView.delegate = self
    profileTableView.dataSource = self
    profileTableView.separatorColor = UIColor.black
    profileTableView.estimatedRowHeight = 140
    profileTableView.rowHeight = UITableViewAutomaticDimension
}


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

func configureSegmentController() {
    segmentBackground.backgroundColor = UIColor(red:0.00, green:0.87, blue:0.39, alpha:1.0)
    profileModelsSegment.center = segmentBackground.center
    profileModelsSegment.setTitle("Profile", forSegmentAt: 0)
    profileModelsSegment.setTitle("Models", forSegmentAt: 1)
    profileModelsSegment.tintColor = UIColor.white
    profileModelsSegment.selectedSegmentIndex = 0
}

func configureText() {
    // make request to firebase, store, and assign to tableView
    sectionTitles = ["Description", "Business Models", "Moats", "Competition", "Unit Economics"]
}

@IBAction func switchAction(_ sender: UISegmentedControl) {
    if sender.selectedSegmentIndex == 0 {
        UIView.animate(withDuration: 0.5, animations: {
            self.profileTableView.alpha = 1
            self.modelsContainerView.alpha = 0
        })
    } else {
        UIView.animate(withDuration: 0.5, animations: {
            self.profileTableView.alpha = 0
            self.modelsContainerView.alpha = 1
        })
    }
}

// TableView Datasource
// Customizes Section Titles
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    // Makes Background White
    let cell = tableView.dequeueReusableCell(withIdentifier: "customHeader") as UITableViewCell!
    cell?.textLabel?.text = sectionTitles[section]
    cell?.contentView.backgroundColor = UIColor.white
    return cell
}

func numberOfSections(in tableView: UITableView) -> Int {
    return sectionTitles.count
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    //configure cell
    let cell = tableView.dequeueReusableCell(withIdentifier: "profileCell", for: indexPath) as! ProfileCell

    cell.mainSectionText.text = "Box (company) Box (formerly Box.net), based in Redwood City, California, is a cloud content management and file sharing service for businesses. The company uses a freemium business model to provide cloud storage and file hosting for personal accounts and businesses."

    return cell
}

}

这是错误消息:

Error Message

我的出口连接:

connection

1 个答案:

答案 0 :(得分:1)

在更新房产之前,您需要初始化segmentBackground。像segmentBackground = UIView()这样的东西就足够了。

如果要从故事板连接UIView,请确保连接有效。在Xcode中,可以通过查看Connections Inspector并确保已配置正确的连接来完成此操作:

enter image description here