我在Swift中成功编译此代码时遇到了问题。它是一个秒表程序。两个标签(开始和停止)和一个文本字段作为输出。它说它是一个委托问题,并以红色突出显示:
class AppDelegate: UIResponder, UIApplicationDelegate
我的项目代码如下:
import UIKit
class ViewController: UIViewController {
var timer = NSTimer()
var counter = 1
@IBOutlet weak var Label: UILabel!
override func viewDidLoad()
{
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func updateCounter()
{
counter += 1
Label.text = String(counter)
}
@IBAction func Start(sender: AnyObject)
{
counter = 0
Label.text = String(counter)
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector ("updateCounter"), userInfo: nil, repeats: true)
}
@IBAction func Stop(sender: AnyObject)
{
timer.invalidate()
}
}
答案 0 :(得分:0)
非常常见的问题,这就是你需要做的事情:
我认为,问题在于您更改了代码中插座的名称,当故事板查找该插座时,它会导致您的应用崩溃。确保在重命名插座时重新连接插座!