Mugunthan Balakrishnan
Mugunthan Balakrishnan

Reputation: 865

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

Upvotes: 0

Views: 450

Answers (1)

VRAwesome
VRAwesome

Reputation: 4803

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

Upvotes: 1

Related Questions