我有一个项目,我已经工作了几个月。一切正常,直到我将Xcode从6.x更新到7.1并将Swift从1.x更新到2.1。我现在遇到的问题如下:
我不确定为什么。我创建了一个新项目,但它在那里是一样的。我已将以下代码粘贴到测试项目中。关于调试我做了这个:Xcode>调试>通过PID或名称附加到处理>添加了我的.com ......但它仍然是一样的。
我更新之前和现在之间项目之间的主要区别是:
但这不会影响,因为我的测试项目遇到了同样的问题。所以这里的问题是当我运行Today小部件(打开它)时,Hello World将计数,当我关闭今天的小部件并重新打开它时,计数器应为0并重新开始计数,但它会卡在当我关闭今天的扩展时我得到的值,当我再次打开它时,viewDidAppear没有被调用。关闭我的意思是我拖动今天的分机/按下主页按钮。
感谢帮助。
我的测试项目的代码:
import UIKit
import NotificationCenter
class TodayViewController: UIViewController, NCWidgetProviding {
var timer = NSTimer()
var counter = 0
@IBOutlet weak var lblText: UILabel!
override func viewDidLoad() {
print("viewDidLoad")
super.viewDidLoad()
// Do any additional setup after loading the view from its nib.
}
override func viewDidAppear(animated: Bool) {
print("viewDidAppear")
super.viewWillAppear(animated)
timer = NSTimer.scheduledTimerWithTimeInterval(3.0, target: self, selector: Selector("getData"), userInfo: nil, repeats: true)
}
override func viewDidDisappear(animated: Bool) {
print("viewDidDisappear")
timer.invalidate()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func widgetPerformUpdateWithCompletionHandler(completionHandler: ((NCUpdateResult) -> Void)) {
// Perform any setup necessary in order to update the view.
// If an error is encountered, use NCUpdateResult.Failed
// If there's no update required, use NCUpdateResult.NoData
// If there's an update, use NCUpdateResult.NewData
completionHandler(NCUpdateResult.NewData)
}
func getData(){
print("getData")
counter+=1
lblText.text = "Hello World \(counter)"
}
}
更新: 看到我错过了一些以下但已更新它们,但它是相同的行为。
override func viewDidLoad() {
super.viewDidLoad()
print("viewDidLoad")
// Do any additional setup after loading the view from its nib.
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
print("viewDidAppear")
print(counter)
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
print("viewWillAppear")
counter = 0
timer = NSTimer.scheduledTimerWithTimeInterval(3.0, target: self, selector: Selector("getData"), userInfo: nil, repeats: true)
}
override func viewDidDisappear(animated: Bool) {
super.viewDidDisappear(animated)
print("viewDidDisappear")
timer.invalidate()
}