UIButton 导致 (lldb) 错误 EXC_BAD_ACCESS
UIButton causes (lldb) error with EXC_BAD_ACCESS
我在 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.
}
}
我把自己项目的相关代码放到空白项目中,错误依旧。
编辑:
看起来 init 是一个特殊关键字 Swift 在桥接 ObjC 代码时使用。来自文档:
Initialization
To instantiate an Objective-C class in Swift, you call one of its
initializers with Swift syntax. When Objective-C init methods come
over to Swift, they take on native Swift initializer syntax. The
“init” prefix gets sliced off and becomes a keyword to indicate that
the method is an initialize
因此,如果您将代码更改为如下所示,它将起作用。
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: "login:", forControlEvents:.TouchUpInside)
}
func login(button: UIButton)
{
println("Logging in...")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
我在 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.
}
}
我把自己项目的相关代码放到空白项目中,错误依旧。
编辑:
看起来 init 是一个特殊关键字 Swift 在桥接 ObjC 代码时使用。来自文档:
Initialization
To instantiate an Objective-C class in Swift, you call one of its initializers with Swift syntax. When Objective-C init methods come over to Swift, they take on native Swift initializer syntax. The “init” prefix gets sliced off and becomes a keyword to indicate that the method is an initialize
因此,如果您将代码更改为如下所示,它将起作用。
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: "login:", forControlEvents:.TouchUpInside)
}
func login(button: UIButton)
{
println("Logging in...")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}