IOS dynamic Add button

Source: Internet
Author: User

IOS dynamic Add button

After you click an existing button, a new button is automatically created and an event is added to the new button. A prompt box is displayed.

Add in viewcontroller. h

@ Property (weak, nonatomic) IBOutletUIButton * addbutton;

Add RESPONSE event addbutton for this button

Add in viewcontroller. m

 

-(IBAction) addButton :( id) sender {

// Add a button dynamically

CGRect frame = CGRectMake (0,0, 300, 50 );

UIButton * button = [UIButtonbuttonWithType: UIButtonTypeRoundedRect];

Button. frame = frame;

[Button setTitle: @ "newly added dynamic button" forState: UIControlStateNormal];

Button. backgroundColor = [UIColor redColor];

Buttons. tag = 2000;

[Button addTarget: selfaction: @ selector (buttonClicked :) forControlEvents: UIControlEventTouchUpInside];

[Self. viewaddSubview: button];

}

// This is the response function of the new button.

-(IBAction) buttonClicked :( id) sender {

UIAlertView * alert = [[UIAlertViewalloc] initWithTitle: @ "prompt"

Message: @ "click the Dynamic Button! "

Delegate: self

CancelButtonTitle: @ "OK"

OtherButtonTitles: nil];

[Alertshow];

}



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.