单击on runtime created按钮会崩溃我的ios应用程序(swift)

时间:2015-02-12 08:54:09

标签: ios swift button

我是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 {

of AppDelegate.swift

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

3 个答案:

答案 0 :(得分:1)

功能不在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

答案 1 :(得分:1)

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

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

你为什么这么问?这是因为您将在函数范围内注册您的事件。当功能结束时,您的事件功能不再存在。从那以后,chrash

答案 2 :(得分:1)

试试这个:

 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