在设置UIView Swift 3

时间:2017-03-09 20:53:44

标签: swift swift3

我正在制作测验应用程序,当我想更改我的UIView的alpha值时,我收到错误fatal error: unexpectedly found nil while unwrapping an Optional value,在它变温之前我正在改造我的应用程序,我认为我删除了一些东西或者误改。我不想把我的所有代码放在这里,因为它很安静,所以我会删除最重要的部分。

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()
    }

}

设置dimViewquestionLabel的字母时出错。我不知道为什么因为它们是通过故事板连接的

PS。当我在此dimView.alpha = 0上设置断点并在控制台中输入po dimview时,它表示dimViewnil

编辑:我有一些有用的信息。当我在调用DimView之前设置questionLabelmodel?.getQuestions()的alpha值时,它会起作用,当我在那里设置断点并在控制台中输入po dimView!时,它会返回一些值。 enter image description here

questionsLabel也有一些东西。 enter image description here

但是当代码进入model?.getQuestions(),然后我输入po dimView!时,它返回nil。 enter image description here

2 个答案:

答案 0 :(得分:2)

在调用setAll(questionsReturned:[Question])之前调用{p> viewDidLoad()。 @IBOutlets直到调用viewDidLoad()之前才连接。

class ViewController: UIViewController {
    @IBOutlet weak var myLabel: UILabel!

    var myText: String = ""

    func viewDidLoad() {
        super.viewDidLoad()
        myLabel.text = myText
    }

    set(myText: String) {
        self.myText = myText
    }
}

像上面这样做......

- 编辑 -

我在屏幕截图中注意到,在与调用displayCurrentQuestion的屏幕截图中加载的视图控制器不同的视图控制器对象上调用viewDidLoad。可能是你在某处创建了第二个视图控制器?

执行此操作...在displayCurrentQuestionviewDidLoad方法的开头添加断点。然后运行你的应用程序您会发现displayCurrentQuestionviewDidLoad之前被称为,或者在viewDidLoad被调用的其他对象上被调用(您可以告诉它)它是self变量的十六进制值的不同对象。)

答案 1 :(得分:0)

在故事板中检查您的视图控制器的IBOutlet连接也很好,可能存在错误的连接