简体   繁体   English

UIButton在Swift中抛出线程1:EXC_BAD_ACCESS(代码= 1,地址0x…)

[英]UIButton throwing Thread1: EXC_BAD_ACCESS (code=1, address 0x…) in Swift

I am creating a UIButton in another file opposed to the main ViewController. 我在与主ViewController相对的另一个文件中创建UIButton。 I created this button 我创建了这个按钮

var newNoteButton = UIButton(frame: CGRectMake(5, 18, 152.5, 37))
newNoteButton.backgroundColor = UIColor.grayColor()
newNoteButton.addTarget(self, action: ("newNoteButtonAction:"), forControlEvents: UIControlEvents.TouchUpInside)
newNoteButton.setTitle("New Note", forState: UIControlState.Normal)
newNoteButton.userInteractionEnabled = true
self.view.addSubview(newNoteButton)

with action 行动起来

func newNoteButtonAction (sender: UIButton!){
    println("New Note")
}

It is throwing the above error even though, if i copy and paste the same code into my ViewController, it doesn't flag me at all. 即使我将相同的代码复制并粘贴到ViewController中,它也会引发上述错误,它根本不会标记我。 Why is it doing this? 为什么这样做呢? It is meant to just print out the string "New Note" but something causes Thread 1 to queue. 它的意思是只打印出字符串“ New Note”,但是会导致线程1排队。

Edit: After a bit of reading, I have been able to reduce the amount of code to just this: 编辑:经过一番阅读,我已经能够减少代码量为:

    var newNoteButton = UIButton(frame: CGRectMake(5, 18, 152.5, 37))
    newNoteButton.backgroundColor = UIColor.grayColor()
    newNoteButton.addTarget(self, action: ("newNoteButtonAction:"), forControlEvents: UIControlEvents.TouchUpInside)

and this: 和这个:

func newNoteButtonAction (sender: UIButton!){
    println("New Note")
}

I tried removing the action and the problem was still there. 我尝试删除操作,但问题仍然存在。 This exists within my textView class in a separate file. 这存在于我的textView类中的单独文件中。 In my AppDelegate file, the Root View Controller is ViewController. 在我的AppDelegate文件中,根视图控制器是ViewController。 If I move the button to ViewController, the issue does not present itself. 如果我将按钮移至ViewController,则不会出现此问题。 The only code in my ViewController is this 我的ViewController中唯一的代码是这样的

import Foundation
import UIKit

class ViewController: UIViewController, UITextViewDelegate, UIScrollViewDelegate {

init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
    super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}
//Global Variables
var scrollView = UIScrollView()

override func viewDidLoad(){
    super.viewDidLoad()

    //Add textView
    //let textViewRef = textView() Removed to test
    //self.view.addSubview(textViewRef.view)

    //Button Code
    var newNoteButton = UIButton(frame: CGRectMake(5, 18, 152.5, 37))
    newNoteButton.backgroundColor = UIColor.grayColor()
    newNoteButton.addTarget(self, action: ("newNoteButtonAction:"), forControlEvents: UIControlEvents.TouchUpInside)
    self.view.addSubview(newNoteButton)

}

func newNoteButtonAction (sender: UIButton!){
    println("New Note")
}

} }

I have tried messing about with conditionals and the semicolon in the action but that doesn't seem to affect it or help in any way. 我已经尝试过在操作中使用条件语句和分号,但是这似乎并没有影响或以任何方式提供帮助。 If you need any more information, please feel free to ask. 如果您需要更多信息,请随时询问。

Edit 2 编辑2

My other View Controller looks like this: 我的其他View Controller如下所示:

import Foundation
import UIKit
class textView: UIViewController {
//Globals
var newNoteButton = UIButton()

override func viewDidLoad() 

    newNoteButton = UIButton(frame: CGRectMake(5, 18, 152.5, 37))
    newNoteButton.backgroundColor = UIColor.grayColor()
    newNoteButton.addTarget(self, action: ("newNoteButtonAction:"), forControlEvents: UIControlEvents.TouchUpInside)
    self.view.addSubview(newNoteButton)

}

func newNoteButtonAction (sender: UIButton!){
    println("New Note")
}
}

Edit 3: I just noticed that this information might be a bit relevant. 编辑3:我刚刚注意到此信息可能有点相关。 The error is on the class AppDelegate line in my delegate file. 错误在我的委托文件的class AppDelegate

I had the same problem and I received the same error that you. 我遇到了同样的问题,并且收到了与您相同的错误。 I wanted create a button in other class. 我想在其他班级创建一个按钮。 I recommend you read my solution: How invoke a method in the method addTarget of the Button class when I create programmatically the button in a plain class in Swift . 我建议您阅读我的解决方案: 在Swift的普通类中以编程方式创建按钮时,如何在Button类的addTarget方法中调用方法 An abstract, you should inherit of the "UIClass" that you require. 摘要,您应该继承所需的“ UIClass”。 If you want to create a button, you should inherit of "UIButton" class. 如果要创建按钮,则应继承“ UIButton”类。 Then, you set the values of the attributes of this class instead of create a button. 然后,您设置此类的属性值,而不是创建按钮。

FYI for anybody with this issue in the future, it can usually be debugged back to the retention of the button within the class you are adding the target to. 仅供参考,以后遇到此问题的任何人,通常都可以将其调试回添加到目标的类中按钮的保留位置。

Try moving the declaration of the button or targeted class up to a class global to be cleaned up after the view is disposed of. 尝试将按钮或目标类的声明移至要清除视图后要清除的全局类。

Example: In the instance listed above, declare the newNoteButton at the class level instead of inside the viewDidLoad function to ensure it's retained while it can call it's target. 示例:在上面列出的实例中,在类级别而不是在viewDidLoad函数内部声明newNoteButton ,以确保在调用它的目标时保留它。

class ViewController: UIViewController, UITextViewDelegate, UIScrollViewDelegate {

    // Global Variables
    var scrollView = UIScrollView()
    var newNoteButton: UIButton! // <- Declare button here instead of in viewDidLoad

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM