swift - 将容器 View 添加到代码时发生 fatal error

标签 swift xcode option-type fatal-error uicontainerview

<分区>

我意识到有人问过关于 fatal error 和解包选项的类似问题,但在这种情况下,我添加到我的代码中的唯一东西是两个 socket (我已经检查了它们的连接),现在我在一些地方遇到了错误以前没有引发错误。

我的代码运行良好,然后我添加了两个容器 View ,它崩溃并提示:“ fatal error :在展开可选值时意外发现 nil”,但我不知道可选值在哪里。它说它与一个名为 segmentBackground 的 UIview 有关,但在我添加容器 View 之前它没有引发任何错误。

感谢您提前提供的帮助。

这是我的代码:

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

我的 socket 连接:

connection

最佳答案

您需要先初始化您的segmentBackground,然后才能更新属性。像 segmentBackground = UIView() 这样的东西就足够了。

如果您从 Storyboard连接 UIView,请确保连接有效。在 Xcode 中,这可以通过查看连接检查器并确保已配置正确的连接来完成:

enter image description here

关于swift - 将容器 View 添加到代码时发生 fatal error ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46962680/

相关文章:

ios - UIPickerView 方法将 UIPickerView 对象作为参数但未使用

ios - 将 Swift 框架集成到 objective-c 应用程序

swift - 我如何从 NSDate 中找到一周的开始?

ios - 更新应用程序后 UILocalNotification 自定义声音无法正常工作

ios - UIPresentationController - 演示 View 大小?

ios - 如何在 swift 中使用不同的闭包类型

ios - 在 ScrollView 中放置 View 和元素

java - 如何使用 Java 可选运算符映射多个子类型

Swift 可选初始化

ios - [CLLocationCoordinate2d]?没有名为下标的成员