首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Swift:其视图不在窗口层次结构中

Swift:其视图不在窗口层次结构中
EN

Stack Overflow用户
提问于 2016-05-27 04:06:07
回答 1查看 892关注 0票数 0

我知道这是很多要求,但我已经尝试了很多其他的解决方案,我似乎不能正确的。

所以,我有一个类来计数,在倒计时结束时,一个新的视图开始了。

这是倒计时班:

代码语言:javascript
复制
import Foundation
import UIKit

class CountdownController: UIViewController {

// MARK: Properties
@IBOutlet weak var countDownLabel: UILabel!


var count = 3

override func viewDidLoad() {
    super.viewDidLoad()


}

override func viewDidAppear(animated: Bool) {
            var timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("update"), userInfo: nil, repeats: true)
}

func update() {
    if(count > 0) {
        countDownLabel.text = String(count--)
    }
    else {
        self.performSegueWithIdentifier("goestoMathTest", sender: self)

    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// MARK: Actions

}

在MathTestController显示之后出现的错误是:

代码语言:javascript
复制
2016-05-26 23:43:48.579 TraderMathTestiOS[18654:951105] Warning: Attempt to 
present <TraderMathTestiOS.MathTestController: 0x7fca824be7d0> on 
<TraderMathTestiOS.CountdownController: 0x7fca824b9d70> whose view is not in 
the window hierarchy!

**编辑:所以我尝试了另一个改变一些事情,我想我缩小了问题。我将计时器更改为viewDidLoad()中的计时器,并将重复更改为'false‘,现在MathTestController在1秒后出现,没有出现警告。以下是修改后的代码:

代码语言:javascript
复制
override func viewDidLoad() {
    super.viewDidLoad()
            var timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("update"), userInfo: nil, repeats: false)

}


func update() {
    self.performSegueWithIdentifier("goestoMathTest", sender: self)

    if(count > 0) {
        countDownLabel.text = String(count--)
    }
    else {
        self.performSegueWithIdentifier("goestoMathTest", sender: self)

    }
}

我认为出现错误的原因是,即使在调用CountdownController之后,计时器仍会在MathTestController中重复。有人知道如何在没有错误的情况下获得我最初的功能(计数'3,2,1‘的计时器)?也许我需要用某种方式杀死计时器?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-05-27 05:54:08

我终于修好了这个。对于任何想知道,如果您的计时器重复,您需要使它无效,如果您要启动一个新的视图。这是固定代码:

代码语言:javascript
复制
class CountdownController: UIViewController {

// MARK: Properties
@IBOutlet weak var countDownLabel: UILabel!
var timer = NSTimer()


var count = 3

override func viewDidLoad() {
    super.viewDidLoad()
            timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("update"), userInfo: nil, repeats: true)

}


func update() {

    if(count > 0) {
        countDownLabel.text = String(count--)
    }
    else {
        timer.invalidate()
        self.performSegueWithIdentifier("goestoMathTest", sender: self)

    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37474681

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档