如何确定UIActionSheet上是否按下了取消按钮?
我的UIActionSheet设置如下:
-(IBAction)fileButtonPressed
{
UIActionSheet *mymenu = [[UIActionSheet alloc]
initWithTitle:@"Select Folder"
delegate:self
cancelButtonTitle:@"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:nil];
for (i=0; i<3; i++)
{
[mymenu addButtonWithTitle:@"Button Name"];
}
[mymenu showInView:self.view];
}
我遇到的问题是我无法区分取消按钮和所选的第一个按钮。
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *option = [actionSheet buttonTitleAtIndex:buttonIndex];
//buttonIndex == 0 if the cancel button is pressed or
//if the first item is pressed.
}
有没有更好的方法来设置它?
答案 0 :(得分:31)
诀窍是不使用自动取消按钮,而是自己添加。
另一个小问题是在结尾而不是在开头添加取消按钮。
-(IBAction)fileButtonPressed
{
UIActionSheet *mymenu = [[UIActionSheet alloc]
initWithTitle:@"Select Folder"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
for (int nb=0; nb<3; nb++)
{
[mymenu addButtonWithTitle:@"Button Name"];
}
mymenu.cancelButtonIndex = [mymenu addButtonWithTitle: @"Cancel"];
[mymenu showInView:self.view];
}
将此stackoverflow条目归功于答案。
答案 1 :(得分:16)
if (buttonIndex == actionSheet.cancelButtonIndex)
{
// Handle cancel action
}
UIActionSheet还有destructiveButtonIndex
和firstOtherButtonIndex
等属性进行比较。
答案 2 :(得分:1)
添加此
[mymenu showInView:self.parentViewController.tabBarController.view];