简体   繁体   English

如何在 ViewControllers 中以编程方式添加 UIButton

[英]How to add UIButton programmatically in ViewControllers

I have two view controllers, ViewControllerA and ViewControllerB, where ViewControllerB is a subclass of ViewControllerA.我有两个视图控制器,ViewControllerA 和 ViewControllerB,其中 ViewControllerB 是 ViewControllerA 的子类。 Here is the code implementation in ViewControllerA.下面是 ViewControllerA 中的代码实现。

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();
}

In my ViewControllerB I would like to use createButton() but not the way it was used in ViewControllerA.在我的 ViewControllerB 我想使用 createButton() 但不是它在 ViewControllerA 中使用的方式。 Let's say I want to have red color not blue in ViewControllerB in createButton().假设我想在 createButton() 的 ViewControllerB 中使用红色而不是蓝色。 How can I override createButton() to be different in ViewControllerB?如何在 ViewControllerB 中覆盖 createButton() 以使其不同?

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.希望可以帮助那些也试图理解视图控制器子类化的最佳方式的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM