简体   繁体   English

如何在iphone中的UIActionSheet中动态添加按钮

[英]How to add button dynamically in UIActionSheet in iphone

我需要在iphone中的UIActionSheet中动态添加按钮。请帮帮我。

Just alloc and init a new UIActionSheet instance and add the buttons one after another using –addButtonWithTitle: . 只需分配并初始化一个新的UIActionSheet实例,并使用–addButtonWithTitle:一个接一个地添加按钮–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. 然后,您可以通过-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): 下面是一个示例,如果布尔值useDestructiveButtonYES ,则添加一个按钮并添加另一个按钮(并直接将其设置为破坏性按钮,使其useDestructiveButton红色):

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. 不要忘记调用适当的show方法。

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. 还要记住,您的父控制器必须符合UIActionSheetDelegate协议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM