【问题标题】:UIButton causes (lldb) error with EXC_BAD_ACCESSUIButton 导致 (lldb) 错误与 EXC_BAD_ACCESS
【发布时间】: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 很特别:/

标签: ios swift uibutton uikit


【解决方案1】:

编辑:

看起来 init 是 Swift 在桥接 ObjC 代码时使用的一个特殊关键字。来自文档:

初始化

要在 Swift 中实例化一个 Objective-C 类,你可以调用它的一个 使用 Swift 语法的初始化器。当 Objective-C 的 init 方法出现时 对于 Swift,它们采用原生 Swift 初始化语法。这 “init”前缀被切掉并成为一个关键字来表明 该方法是一个初始化

因此,如果您将代码更改为如下所示,它将起作用。

感谢@cedabob 提供参考:https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/InteractingWithObjective-CAPIs.html#//apple_ref/doc/uid/TP40014216-CH4-XID_26

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.
        }
    }

【讨论】:

  • ...不幸的是,这是正确的。不知道为什么。斯威夫特有时很生气。感谢您的帮助!
  • 来自 swift 文档:““init”前缀被切掉,成为一个关键字,表明该方法是一个初始化器。” developer.apple.com/library/ios/documentation/Swift/Conceptual/…
  • 感谢@chedabob 的链接 - 编辑了原始答案以包含
  • @chedabob 感谢您的参考。这对于一个只有 swift 的项目来说是非常违反直觉的。
  • 相同的编译器,我猜这就是它发生的原因。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多