问题描述
我正在制作测验应用程序,当我想更改我的 UIView 的 alpha 值时,我收到错误 fatal error:unwrapping an Optional value 时意外发现 nil
,在它变热之前我正在重塑我的应用程序,我认为我删除了某些内容或错误地更改了内容.我不想把我所有的代码都放在这里,因为它很安静,所以我会剪掉最重要的部分.
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()
}
}
我在设置 dimView
和 questionLabel
的 alpha 时出错.我不知道为什么,因为它们是由故事板连接的
im getting error while setting alpha of dimView
and questionLabel
. I don't know why because they are connected by the storyboard
附注.当我在这个 dimView.alpha = 0
上设置断点并在控制台中输入 po dimview
它说 dimView
是 nil
>
PS. When I set breakpoint on this dimView.alpha = 0
and type po dimview
in console it says that dimView
is nil
我有一些有用的信息.当我在调用 model?.getQuestions()
之前设置 DimView
和 questionLabel
的 alpha 时,它可以工作,当我在那里设置断点并输入 po dimView!
进入控制台,它返回一些值.
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.
和 questionsLabel
也有一些东西.
但是当代码进入model?.getQuestions()
,然后我输入po dimView!
它返回nil.
But when code goes into model?.getQuestions()
, and then I type po dimView!
it returns nil.
推荐答案
setAll(questionsReturned:[Question])
在 viewDidLoad()
被调用之前被调用.@IBOutlets 直到 viewDidLoad()
被调用之前才会连接.
setAll(questionsReturned:[Question])
is getting called before viewDidLoad()
is called. The @IBOutlets aren't connected until just before viewDidLoad()
is called.
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
的屏幕截图中加载的视图控制器不同的视图控制器对象上调用的.可能是您正在某处创建第二个视图控制器?
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 where viewDidLoad
is called. It could be that you are creating a second view controller somewhere?
这样做... 在 displayCurrentQuestion
和 viewDidLoad
方法的开头放置断点.然后运行您的应用程序.您会发现 displayCurrentQuestion
在 before viewDidLoad
被调用,或者在 viewDidLoad
的不同对象上被调用code> 被调用(你可以通过 self
变量的十六进制值判断它是一个不同的对象.)
Do this... Put break points at the beginning of your displayCurrentQuestion
and viewDidLoad
methods. Then run your app. You will find that either displayCurrentQuestion
is getting called before viewDidLoad
is, or it is being called on a different object that viewDidLoad
was called on (you can tell it's a different object by the hex value of the self
variable.)
这篇关于设置 UIView Swift 3 的 alpha 时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!