正在获取:“在进行演示或关闭时,尝试从视图控制器中关闭”

时间:2019-01-21 02:49:02

标签: ios swift uiviewcontroller segue dismiss

  1. Swift 4测验应用程序。
  2. 第一个视图-选择要解决的问题数。 (使用segue进行第二次查看)
  3. 第二视图-开始测验->已检查答案,并将“正确”或“错误”消息发送给第三视图。 (使用segue方式进行第三次查看)

出现第三个视图时出现错误:

  

在演示或关闭过程中,尝试从视图控制器中退出!

我尝试使用:

DispatchQueue.main.asyncAfter(deadline: .now() + 1.0, execute:....

警告消息消失了,但问题是我无权访问从第二个视图中获得的所有变量以用于第三个视图。

override func viewDidLoad() {
    super.viewDidLoad()

    DispatchQueue.main.asyncAfter(deadline: .now() + 1.0, execute: {
        if self.answerStatus == "Correct!" {
            self.correctWrongStatusLabel.text = self.answerStatus
            self.praiseLabel.text = "Good job!"
            self.correctAnswerIsLabel.text = ""
            let timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(self.fireTimer), userInfo: nil, repeats: false)
            self.fireTimer()

        } else if self.answerStatus == "Wrong!" {
            self.correctWrongStatusLabel.text = self.answerStatus
            self.praiseLabel.text = "Maybe next time!"
            self.correctAnswerIsLabel.text = "回答 : \(self.correctAnswerIs)"
            let timer = Timer.scheduledTimer(timeInterval: 3.0, target: self, selector: #selector(self.fireTimer), userInfo: nil, repeats: false)
            self.fireTimer()
        }
    })

@objc func fireTimer() {
    delegate?.answerStatusReceived(answerStatusString: answerStatusString)
    self.dismiss(animated: true, completion: nil)
}

第二个视图随测验出现后,当用户选择答案时,它将针对正确答案进行检查并显示回答,指出答案是正确还是错误。显示XX秒后,响应视图必须自动关闭。

3 个答案:

答案 0 :(得分:0)

您是否尝试删除DispatchQueue方法?

我会这样:

    override func viewDidLoad() {
        super.viewDidLoad()

            if self.answerStatus == "Correct!" {
                self.correctWrongStatusLabel.text = self.answerStatus
                self.praiseLabel.text = "Good job!"
                self.correctAnswerIsLabel.text = ""
                let timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(self.fireTimer), userInfo: nil, repeats: false)
                self.fireTimer()

            } else if self.answerStatus == "Wrong!" {
                self.correctWrongStatusLabel.text = self.answerStatus
                self.praiseLabel.text = "Maybe next time!"
                self.correctAnswerIsLabel.text = "回答 : \(self.correctAnswerIs)"
                let timer = Timer.scheduledTimer(timeInterval: 3.0, target: self, selector: #selector(self.fireTimer), userInfo: nil, repeats: false)
                self.fireTimer()
            }
}

    @objc func fireTimer() {
        delegate?.answerStatusReceived(answerStatusString: answerStatusString)
        dismiss(animated: true, completion: nil)
    }

我认为您在viewDidLoad方法中包含了FireTimer()函数,因此我将在函数之前的行中添加一个括号“}”。

让我知道它是否有效! :)

答案 1 :(得分:0)

仅使用:-关闭(动画:true,完成:nil)并忽略自身

答案 2 :(得分:0)

我认为即使添加延迟但不能保证,也无法在加载时关闭VC。尝试在viewDidAppear中添加计时器。另外,您的self.fireTimer()也不是必需的。

override func viewDidLoad() {
    super.viewDidLoad()

    if self.answerStatus == "Correct!" {
        self.correctWrongStatusLabel.text = self.answerStatus
        self.praiseLabel.text = "Good job!"
        self.correctAnswerIsLabel.text = ""
    } else if self.answerStatus == "Wrong!" {
        self.correctWrongStatusLabel.text = self.answerStatus
        self.praiseLabel.text = "Maybe next time!"
        self.correctAnswerIsLabel.text = "回答 : \(self.correctAnswerIs)"
    }
}

override func viewDidAppear() {
    super.viewDidAppear()

    if self.answerStatus == "Correct!" {
        let timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(self.fireTimer), userInfo: nil, repeats: false)
    } else if self.answerStatus == "Wrong!" {
        let timer = Timer.scheduledTimer(timeInterval: 3.0, target: self, selector: #selector(self.fireTimer), userInfo: nil, repeats: false)
    }
}

@objc func fireTimer() {
    delegate?.answerStatusReceived(answerStatusString: answerStatusString)
    self.dismiss(animated: true, completion: nil)
}