单击运行时创建的按钮会使我的 ios 应用程序崩溃 (swift)

clicking a on runtime created button crashes my ios app (swift)

我是 ios 开发的新手,我面临以下问题。

我在运行时在 viewDidLoad 方法中创建了一个按钮:

class ViewController: UIViewController {

    @IBOutlet weak var TestButton: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        println("start");


        // Create Button at runtime
        var button   = UIButton.buttonWithType(UIButtonType.System) as UIButton
        button.frame = CGRectMake(100, 100, 100, 50)
        button.backgroundColor = UIColor.redColor()
        button.setTitle("Test Button", forState: UIControlState.Normal)
        button.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)

        self.view.addSubview(button)

        func buttonAction(sender:UIButton!)
        {
            println("Button tapped.")
        }



    }

当我在模拟器中按下按钮时,应用程序停止在行:

class AppDelegate: UIResponder, UIApplicationDelegate {

共 AppDelegate.swift

有谁知道为什么它不输出 "Button tapped." 吗? 如果我遇到这样的问题,我该如何将错误消息报告给其他人?我的意思是我在 XCode 中没有看到任何错误代码或堆栈跟踪。在哪里可以找到这个?

函数不在 viewDidLoad 中。 示例:

class ViewController: UIViewController {

@IBOutlet weak var TestButton: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    println("start");


    // Create Button at runtime
    var button   = UIButton.buttonWithType(UIButtonType.System) as UIButton
    button.frame = CGRectMake(100, 100, 100, 50)
    button.backgroundColor = UIColor.redColor()
    button.setTitle("Test Button", forState: UIControlState.Normal)
    button.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)

    self.view.addSubview(button)

    } // CLOSE

    func buttonAction(sender:UIButton!)
    {
        println("Button tapped.")
    }

}// CLOSE

您应该将按钮方法提取到您的函数之外,如下所示:

class ViewController: UIViewController {

    @IBOutlet weak var TestButton: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        println("start");


        // Create Button at runtime
        var button   = UIButton.buttonWithType(UIButtonType.System) as UIButton
        button.frame = CGRectMake(100, 100, 100, 50)
        button.backgroundColor = UIColor.redColor()
        button.setTitle("Test Button", forState: UIControlState.Normal)
        button.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)

        self.view.addSubview(button)





    }

    func buttonAction(sender:UIButton!)
    {
       println("Button tapped.")
    }
}

您可能会问这是为什么?这是因为您将注册该功能范围内的事件。当功能结束时,您的事件功能将不再存在。因为,崩溃

试试这个:

 override func viewDidLoad() {

    super.viewDidLoad()

    let button   = UIButton.buttonWithType(UIButtonType.System) as UIButton
    button.frame = CGRectMake(100, 100, 100, 50)
    button.backgroundColor = UIColor.greenColor()
    button.setTitle("Test Button", forState: UIControlState.Normal)
    button.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)

    self.view.addSubview(button)
}  

func buttonAction(sender:UIButton!)
{
    println("Button tapped")
}

还有import UIKit