简体   繁体   English

如何从NSObject类以编程方式添加UIButton

[英]How to add UIButton Programmatically from a NSObject Class

I am trying to create a UIButton in a NSObject class. 我正在尝试在NSObject类中创建UIButton。 I get the error " Use of unresolved identifier view. I figured that it needs to have UIViewController for it to work. 我收到错误“使用未解决的标识符视图。我认为它需要具有UIViewController才能起作用。

is there a work around so that I create multiple buttons with just this on multiple view controllers. 是否有解决方法,所以我在多个视图控制器上仅以此创建多个按钮。 what am I missing? 我想念什么?

func createButton () {
    let button = UIButton();
    button.setTitle("Add", forState: .Normal)
    button.setTitleColor(UIColor.blueColor(), forState: .Normal)
    button.frame = CGRectMake(15, -50, 200, 100)
    view.addSubview(button)
}

Declare method definition with parameter-argument in which you want to add button : 使用要在其中添加button的参数参数声明方法定义:

func createButton (view:UIView) {
    let button = UIButton();
    button.setTitle("Add", forState: .Normal)
    button.setTitleColor(UIColor.blueColor(), forState: .Normal)
    button.frame = CGRectMake(15, -50, 200, 100)
    view.addSubview(button)
}

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

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