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

How to add UIButton Programmatically from a NSObject Class

我正在尝试在 NSObject class 中创建一个 UIButton。我收到错误“使用未解析的标识符视图。我认为它需要 UIViewController 才能工作。

是否有解决方法,以便我在多个视图控制器上创建多个按钮。我错过了什么?

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

使用要在其中添加按钮的参数参数声明方法定义:

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