cfischer
cfischer

Reputation: 24892

Creating controls on runtime with Cocoa Touch

I have this action for the "Touch Up Inside" event of a UIButton that should create a new UIButton right below the sender:

-(IBAction) cloneMe: (id) sender{

    if (!currentY) {
        currentY = [sender frame].origin.y;
    }

    UIButton *clone = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    CGRect cloneFrame = [sender frame];
    cloneFrame.origin.y += currentY + cloneFrame.size.height + 30;
    clone.frame = cloneFrame;

    currentY = cloneFrame.origin.y + cloneFrame.size.height;


}

It doesn't work and the new button is never displayed. Anybody knows what's going on?

Upvotes: 1

Views: 493

Answers (2)

nacho4d
nacho4d

Reputation: 45078

You are forgetting [sender.superview addSubview:clone];

Upvotes: 2

Vladimir
Vladimir

Reputation: 170829

You create button but don't add it to view hierarchy - you missing

[self.view addSubView: clone];

call or similar

Upvotes: 1

Related Questions