2017-03-09 69 views
-1

我正在測驗應用程序,當我想改變我的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() 
    } 

} 

即時得到誤差而設定的dimViewquestionLabel阿爾法。我不知道爲什麼,因爲他們連接的故事板

PS。當我設置斷點在這個dimView.alpha = 0和類型po dimview在控制檯它說,dimViewnil

編輯:我有一些信息,這可能是有用的。當我設置DimViewquestionLabel的alpha時,在我呼叫model?.getQuestions()之前它會起作用,當我在那裏設置斷點並在控制檯中輸入po dimView!時,它會返回一些值。 enter image description here

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

但是當代碼進入model?.getQuestions(),然後我輸入po dimView!它返回nil。 enter image description here

+0

我知道什麼是可選和零,但我不知道它爲什麼爲零,它不應該是 –

+0

因爲你沒有連接你的插座。請閱讀我鏈接的問題的答案。它涵蓋了如何查找和修復這些問題。 – rmaddy

+0

我寫回放被刪除的答案,我檢查了插座 –

回答

2

setAll(questionsReturned:[Question])在調用viewDidLoad()之前被調用。只有在調用viewDidLoad()之前,@IBOutlets纔會連接。

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方法的開始處放置折點。然後運行你的應用。你會發現,無論是displayCurrentQuestion獲取調用之前viewDidLoad是,或者它被稱爲不同的對象,viewDidLoad被稱爲上(你可以分辨出它是由self變量的十六進制值不同的對象。)

+0

我該如何解決這個問題? –

+0

而不是試圖改變'setAll'內的IBOutlets的狀態,用這些信息創建一些屬性(這稱爲模型),然後在模型的'viewDidLoad'中設置IBOutlets。 –

+0

我試圖瞭解我必須做什麼,你的意思是我必須創建另一個文件/函數,我將設置這個值?對不起,我是新來的迅速和編程 –

0

在故事板中檢查IBOutlet與視圖控制器的連接也很好,可能存在錯誤的連接

+0

我檢查了3次:) –

+0

正如我看到你得到的問題與**模型?.getQuestions()** ** onViewDidLoad **並使用** setAll **處理它,所以如果沒有錯誤,IBOutlets連接應該沒問題 – abdullahselek

+0

我編輯了我的文章並添加了一些可用的調試信息 –