简体   繁体   中英

How to add UIButton Programmatically from a NSObject Class

I am trying to create a UIButton in a NSObject class. I get the error " Use of unresolved identifier view. I figured that it needs to have UIViewController for it to work.

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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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