How to add button dynamically in UIActionSheet in iphone

I need to add a button dynamically in a UIActionSheet in iphone. Please help me.

+4
source share
2 answers

Just select and start a new instance of UIActionSheet and add buttons one by one using –addButtonWithTitle: This method returns you the index into which the button was added. You can then set the index of the destructive button through -setDestructiveButtonIndex.

Here is an example that adds one button and adds another if the boolean value of useDestructiveButton is YES (and directly sets it as a destructive button, making it red):

 UIActionSheet *sheet = [[UIActionSheet alloc] init]; [sheet addButtonWithTitle:@"Button 1"]; if (useDestructiveButton) { [sheet setDestructiveButtonIndex:[sheet addButtonWithTitle:@"Button 2"]]; } 

Remember to call the appropriate show method.

+8
source
 UIActionSheet * as=[[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel",@"Cancel") destructiveButtonTitle:nil otherButtonTitles:nil]; [as setTag:0]; [as addButtonWithTitle:NSLocalizedString(@"First Button",@"First Button")]; [as addButtonWithTitle:NSLocalizedString(@"Second Button",@"Second Button")]; [as showInView:someController.view]; [as autorelease]; 

Also remember that your parent controller must comply with the UIActionSheetDelegate protocol.

+2
source

Source: https://habr.com/ru/post/1336793/


All Articles