【发布时间】:2015-05-20 20:19:09
【问题描述】:
我在 SO 上多次看到这个问题,但这些问题的解决方案并没有解决我的问题。
我动态创建一个 UIButton 并将其添加到我的视图中。我为按钮添加了一个目标,这是一个函数。
目标函数运行(即打印语句运行),然后我在控制台中看到 (lldb) 错误,异常断点突出显示 AppDelegate.swift 的 class AppDelegate: UIResponder, UIApplicationDelegate 行,错误为 EXC_BAD_ACCESS (code=1, address=x020000000c),并且应用崩溃。
我知道这通常是由选择器错误引起的(即冒号表示接受参数的函数),但我尝试了所有冒号和非冒号的变体,但无济于事。
感谢任何帮助
来源:
import UIKit
class ViewController: UIViewController {
var loginButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.loginButton = UIButton()
self.loginButton.frame = CGRectMake(10, 40, self.view.bounds.width - 20, 40)
self.loginButton.setTitle("Login", forState: UIControlState.Normal)
self.loginButton.setTitleColor(UIColor.blueColor(), forState: UIControlState.Normal)
self.view.addSubview(self.loginButton)
self.loginButton.addTarget(self, action: "initLogin:", forControlEvents:.TouchUpInside)
}
func initLogin(button: UIButton)
{
println("Logging in...")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
我从我的项目中取出相关代码,放到一个空白项目中,错误依旧。
【问题讨论】:
-
我知道这听起来很愚蠢,但如果你在 forControlEvents: .TouchUpInside 之间放置一个空格?
-
@NekakKinich 不幸的是,这不是问题:) 不过我希望!
-
我知道,但是 XCode 和 Swift 很特别:/