mcfly soft
mcfly soft

Reputation: 11651

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

I am a newby in ios development and I am facing the following problem.

I create a button at runtime in the viewDidLoad method:

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



    }

When I press the button in the simulator the app stops at line :

class AppDelegate: UIResponder, UIApplicationDelegate {

of AppDelegate.swift

Does anyone have any idea why it doesn't output "Button tapped." ? If I get a problem like this, how can I report the errormessage to someone else ? I mean I do not see any errorcode or stacktrace in XCode. Where to find this ?

Upvotes: 0

Views: 700

Answers (3)

KrisJones
KrisJones

Reputation: 188

Try this:

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

Also import UIKit

Upvotes: 1

Miknash
Miknash

Reputation: 7948

You should extract your method for button outside your function like following:

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

Why is this you might ask? It is because you will register your event which is in scope of the function. When function ends your function for event is not there any more. Since, the chrash

Upvotes: 1

MetalHeart2003
MetalHeart2003

Reputation: 382

Function not in viewDidLoad. example:

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

Upvotes: 1

Related Questions