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

4
我需要在 iPhone 的 UIActionSheet 中动态添加按钮。请帮助我。
2个回答

8
只需分配和初始化一个新的UIActionSheet实例,然后使用-addButtonWithTitle:依次添加按钮。此方法将返回您添加的按钮的索引。然后,您可以通过-setDestructiveButtonIndex设置破坏性按钮的索引。
以下是一个示例,如果布尔值useDestructiveButton为YES,则添加一个按钮,并添加另一个按钮(直接将其设置为破坏性按钮,使其变为红色):
UIActionSheet *sheet = [[UIActionSheet alloc] init];
[sheet addButtonWithTitle:@"Button 1"];
if (useDestructiveButton) {
    [sheet setDestructiveButtonIndex:[sheet addButtonWithTitle:@"Button 2"]];
}

不要忘记调用相应的显示方法。

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

同时也要记住,您的父控制器必须符合UIActionSheetDelegate协议。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接