This question already has an answer here:
I'm making quiz app and when I want to change value of alpha of my UIView I'm getting error fatal error: unexpectedly found nil while unwrapping an Optional value
, before it warms fine but I'm remodeling my app and I think that I delete something or change by mistake. I don't want to put all my code here because it is quiet large so I will cut most important parts.
import UIKit
class ViewController: UIViewController{
@IBOutlet weak var questionLabel: UILabel!
@IBOutlet weak var answerStackView: UIStackView!
// Feedback screen
@IBOutlet weak var resultView: UIView!
@IBOutlet weak var dimView: UIView!
@IBOutlet weak var resultLabel: UILabel!
@IBOutlet weak var feedbackLabel: UILabel!
@IBOutlet weak var resultButton: UIButton!
@IBOutlet weak var resultViewBottomConstraint: NSLayoutConstraint!
@IBOutlet weak var resultViewTopConstraint: NSLayoutConstraint!
var currentQuestion:Question?
var model:QuizModel?
var questions = [Question]()
var numberCorrect = 0
override func viewDidLoad() {
super.viewDidLoad()
model = QuizModel()
model?.getQuestions()
}
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nil, bundle: nil)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
func setAll(questionsReturned:[Question]) {
self.loadViewIfNeeded()
// Do any additional setup after loading the view, typically from a nib.
// Hide feedback screen
dimView.alpha = 0
// Call get questions
questions = questionsReturned
// Check if there are questions
if questions.count > 0 {
currentQuestion = questions[0]
// Load state
loadState()
// Display the current question
displayCurrentQuestion()
}
print("Called!")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func displayCurrentQuestion() {
if let actualCurrentQuestion = currentQuestion {
// Set question label to alpha 0
questionLabel.alpha = 0
// Set the question label
questionLabel.text = actualCurrentQuestion.questionText
UIView.animate(withDuration: 1, delay: 0, options: .curveEaseOut, animations: {
self.questionLabel.alpha = 1
}, completion: nil)
// Create the answer buttons and place them into the scrollview
createAnswerButtons()
// Save state
saveState()
}
}
im getting error while setting alpha of dimView
and questionLabel
. I don't know why because they are connected by the storyboard
PS. When I set breakpoint on this dimView.alpha = 0
and type po dimview
in console it says that dimView
is nil
Edit: I have some informations that can be useful. When I set alpha of DimView
and questionLabel
before I call model?.getQuestions()
it works and when I set breakpoint there and type po dimView!
into console it returns some values.
And questionsLabel
have something too.
But when code goes into model?.getQuestions()
, and then I type po dimView!
it returns nil.
setAll(questionsReturned:[Question])
is getting called beforeviewDidLoad()
is called. The @IBOutlets aren't connected until just beforeviewDidLoad()
is called.Do it like the above...
-- Edit --
I notice in your screenshots that
displayCurrentQuestion
is being called on a different view controller object than the view controller that was loaded in the screenshot whereviewDidLoad
is called. It could be that you are creating a second view controller somewhere?Do this... Put break points at the beginning of your
displayCurrentQuestion
andviewDidLoad
methods. Then run your app. You will find that eitherdisplayCurrentQuestion
is getting called beforeviewDidLoad
is, or it is being called on a different object thatviewDidLoad
was called on (you can tell it's a different object by the hex value of theself
variable.)It is also good to check your IBOutlet connections with your viewcontroller in storyboard, there might be wrong connections