【问题标题】:How to add UIButton programmatically in ViewControllers如何在 ViewControllers 中以编程方式添加 UIButton
【发布时间】:2017-02-25 22:27:06
【问题描述】:

我有两个视图控制器,ViewControllerA 和 ViewControllerB,其中 ViewControllerB 是 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();
}

在我的 ViewControllerB 中,我想使用 createButton(),但不是在 ViewControllerA 中使用它的方式。假设我想在 createButton() 中的 ViewControllerB 中使用红色而不是蓝色。如何在 ViewControllerB 中覆盖 createButton() 以使其不同?

【问题讨论】:

  • 通过覆盖它 - override func createButton()。有什么问题?
  • 一种选择是将颜色参数添加到createButton 方法。
  • 马特,我知道按照你的说法覆盖它。如果我以这种方式覆盖,我必须在 ViewControllerB 中拥有相同数量的代码,只是因为 createButton() 方法中的颜色差异。
  • 您可以在 createButton() 函数中设置控制器名称参数,您可以在其中比较此名称并设置适当的颜色。到您的控制器名称。
  • 谢谢大家。这个问题现在对具有相同想法方法的新 iOS 开发人员很有用。

标签: ios swift uiviewcontroller uibutton


【解决方案1】:

以下方法可行。

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

希望可以帮助那些也试图了解视图控制器子类化的最佳方式的人。

【讨论】:

    猜你喜欢
    • 2012-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多