bihmaster bihmaster - 1 month ago 13
Swift Question

How to add UIButton programmatically in ViewControllers

I have two view controllers, ViewControllerA and ViewControllerB, where ViewControllerB is a subclass of ViewControllerA. Here is the code implementation in ViewControllerA.

class ViewControllerA: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
createButton()
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}

func createButton () {
let button = UIButton();
button.setTitle("Add", forState: .Normal)
button.setTitleColor(UIColor.blueColor(), forState: .Normal)
button.frame = CGRectMake(200, 65, 46, 30) // X, Y, width, height
button.addTarget(self, action: "buttonPressed:", forControlEvents: .TouchUpInside)
self.view.addSubview(button)
}

func buttonPressed(sender: UIButton!) {
var alertView = UIAlertView();
alertView.addButtonWithTitle("Done");
alertView.title = "Alert!";
alertView.message = "Button Pressed!!!";
alertView.show();
}


In my ViewControllerB I would like to use createButton() but not the way it was used in ViewControllerA. Let's say I want to have red color not blue in ViewControllerB in createButton(). How can I override createButton() to be different in ViewControllerB?

Answer

The following would work.

class ViewControllerA: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    createButton()
    buttonPressed()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()      
}

func createButton () {
    let button = UIButton();
    button.setTitle("Add", forState: .Normal)
    button.setTitleColor(UIColor.blueColor(), forState: .Normal)
    button.frame = CGRectMake(200, 65, 46, 30) // X, Y, width, height
    button.addTarget(self, action: "buttonPressed:", forControlEvents: .TouchUpInside)
    self.view.addSubview(button)
}

func buttonPressed(sender: UIButton!) {
    var alertView = UIAlertView();
    alertView.addButtonWithTitle("Done");
    alertView.title = "Alert!";
    alertView.message = "Button Pressed!!!";
    alertView.show();
}


class ViewControllerB: ViewControllerA {

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

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()      
}

override func createButton () {
    let button = UIButton();
    button.setTitle("Add", forState: .Normal)
    button.setTitleColor(UIColor.blueColor(), forState: .Normal)
    button.frame = CGRectMake(200, 65, 46, 30) // X, Y, width, height
    button.addTarget(self, action: "buttonPressed:", forControlEvents: .TouchUpInside)
    self.view.addSubview(button)
}

override func buttonPressed(sender: UIButton!) {
    var alertView = UIAlertView();
    alertView.addButtonWithTitle("Done");
    alertView.title = "Alert!";
    alertView.message = "Button Pressed!!!";
    alertView.show();
}

Hopefully can help those who are also trying to understand the best way of view controller subclassing.