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];
}