繁体   English   中英

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

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

我正在尝试在NSObject类中创建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)
}

使用要在其中添加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