i need to add button dynamically in UIActionSheet 开发者_如何学JAVAin iphone.Please help me.
Just alloc and init a new UIActionSheet instance and add the buttons one after another using –addButtonWithTitle:
. This method returns you the index at which the button has been added to. You can then set the Index of the destructive button via -setDestructiveButtonIndex.
Here is an example that adds one button and adds another one if the boolean value useDestructiveButton
is YES
(and directly sets it as destructive button, making it red):
UIActionSheet *sheet = [[UIActionSheet alloc] init];
[sheet addButtonWithTitle:@"Button 1"];
if (useDestructiveButton) {
[sheet setDestructiveButtonIndex:[sheet addButtonWithTitle:@"Button 2"]];
}
Don't forget to call the appropriate show method.
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 has to conform to the UIActionSheetDelegate protocol.
精彩评论