繁体   English   中英

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

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

我在与主ViewController相对的另一个文件中创建UIButton。 我创建了这个按钮

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)

行动起来

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

即使我将相同的代码复制并粘贴到ViewController中,它也会引发上述错误,它根本不会标记我。 为什么这样做呢? 它的意思是只打印出字符串“ New Note”,但是会导致线程1排队。

编辑:经过一番阅读,我已经能够减少代码量为:

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

和这个:

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

我尝试删除操作,但问题仍然存在。 这存在于我的textView类中的单独文件中。 在我的AppDelegate文件中,根视图控制器是ViewController。 如果我将按钮移至ViewController,则不会出现此问题。 我的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")
}

}

我已经尝试过在操作中使用条件语句和分号,但是这似乎并没有影响或以任何方式提供帮助。 如果您需要更多信息,请随时询问。

编辑2

我的其他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")
}
}

编辑3:我刚刚注意到此信息可能有点相关。 错误在我的委托文件的class AppDelegate

我遇到了同样的问题,并且收到了与您相同的错误。 我想在其他班级创建一个按钮。 我建议您阅读我的解决方案: 在Swift的普通类中以编程方式创建按钮时,如何在Button类的addTarget方法中调用方法 摘要,您应该继承所需的“ UIClass”。 如果要创建按钮,则应继承“ UIButton”类。 然后,您设置此类的属性值,而不是创建按钮。

仅供参考,以后遇到此问题的任何人,通常都可以将其调试回添加到目标的类中按钮的保留位置。

尝试将按钮或目标类的声明移至要清除视图后要清除的全局类。

示例:在上面列出的实例中,在类级别而不是在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