Reputation: 11651
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
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
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
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