我正在測驗應用程序,當我想改變我的UIView的alpha值我得到錯誤fatal error: unexpectedly found nil while unwrapping an Optional value
,在它溫暖好但我正在重塑我的應用程序和我認爲我刪除了某些東西或者錯誤地改變了。我不想把所有的代碼放在這裏,因爲它很安靜,所以我會剪掉最重要的部分。設置UIView的alpha時出錯3
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()
}
}
即時得到誤差而設定的dimView
和questionLabel
阿爾法。我不知道爲什麼,因爲他們連接的故事板
PS。當我設置斷點在這個dimView.alpha = 0
和類型po dimview
在控制檯它說,dimView
是nil
編輯:我有一些信息,這可能是有用的。當我設置DimView
和questionLabel
的alpha時,在我呼叫model?.getQuestions()
之前它會起作用,當我在那裏設置斷點並在控制檯中輸入po dimView!
時,它會返回一些值。
但是當代碼進入model?.getQuestions()
,然後我輸入po dimView!
它返回nil。
我知道什麼是可選和零,但我不知道它爲什麼爲零,它不應該是 –
因爲你沒有連接你的插座。請閱讀我鏈接的問題的答案。它涵蓋了如何查找和修復這些問題。 – rmaddy
我寫回放被刪除的答案,我檢查了插座 –