I realize similar question about fatal errors and unwrapping optionals have been asked, but in this case the only things I have added to my code are two outlets (I've checked their connections) and now I am experiencing errors in places that previously didn't set off errors.
My code was running well and then I added two container views and it crashes saying: "fatal error: unexpectedly found nil while unwrapping an Optional value," but I can't figure out where the optional could be. It says it has to do with a UIview called segmentBackground but that didn't set off any error before I added the container views.
Thank you for your help in advanced.
Here is my code:
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
}
}
And this is the error message:
My outlet connection: