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

Nit*_*tin 4 iphone

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

Bjö*_*lek 8

只需分配并初始化一个新的UIActionSheet实例,然后依次添加按钮–addButtonWithTitle:.此方法返回添加按钮的索引.然后,您可以通过-setDestructiveButtonIndex设置破坏性按钮的索引.

下面是添加一个按钮,如果布尔值增加了另外一个示例useDestructiveButtonYES(并直接将其设定为破坏性按钮,使它红色):

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

不要忘记调用适当的show方法.