I'm creating an object detection app and I currently have it generating bounding boxes around the results. I'd like to create a button each time a new object is detected to be able to navigate to a description page for that object. I generate the observations in a file outside of my View Controller but I'm creating the button in the ViewController. I know the title is not nil but I get a fatal error when I add a subview. Once an object is detected, I call:
And then in the View Controller it runs:
Finally, I receive this error once an object is detected:
I've researched the problem and have found solutions (such as making sure the button is properly linked from the storyboard) but they don't seem to apply since I want to create the button programmatically.
I've also had suggestions that resultView is not initialized at the time that showDetailView() is called but it is displaying on the screen before I try to detect an object.
Code Block swift ViewController().showDetailView(buildingName: String(observationName))
And then in the View Controller it runs:
Code Block swift public func showDetailView(buildingName: String){ let detailViewButton = UIButton() detailViewButton.setTitle(buildingName, for: .normal) detailViewButton.frame = CGRect(x:0, y:0, width:200, height: 100) resultView.addSubview(detailViewButton) _ = Timer.scheduledTimer(withTimeInterval: 10.0, repeats: false) { timer in detailViewButton.removeFromSuperview() } }
Finally, I receive this error once an object is detected:
Code Block swift Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
I've researched the problem and have found solutions (such as making sure the button is properly linked from the storyboard) but they don't seem to apply since I want to create the button programmatically.
I've also had suggestions that resultView is not initialized at the time that showDetailView() is called but it is displaying on the screen before I try to detect an object.