Iphone 按下“取消”按钮不会缩回

Iphone 按下“取消”按钮不会缩回,iphone,objective-c,cocoa-touch,ios,Iphone,Objective C,Cocoa Touch,Ios,我正在实施一项行动计划。当我按ok按钮时,执行这些操作,然后按cancel返回。ok按钮工作正常,但当我按下cancel按钮时,什么也没有发生,它没有收回或做任何事情,只是挂在actionsheet视图上 下面是我的代码: 要在导航栏上创建按钮,请执行以下操作: UIBarButtonItem *clearButton = [[[UIBarButtonItem alloc] initWithTitle:@"Clear History"

我正在实施一项行动计划。当我按ok按钮时,执行这些操作,然后按cancel返回。ok按钮工作正常,但当我按下cancel按钮时,什么也没有发生,它没有收回或做任何事情,只是挂在actionsheet视图上

下面是我的代码:

要在导航栏上创建按钮,请执行以下操作:

UIBarButtonItem *clearButton = [[[UIBarButtonItem alloc] initWithTitle:@"Clear     History"
                                                                      style:UIBarButtonItemStyleBordered
                                                              target:self
                                                              action:@selector(ClearHistoryAction:)] autorelease];
self.navigationItem.leftBarButtonItem = clearButton;
当我单击并启动操作表时:

   - (IBAction)ClearHistoryAction:(id)sender
  {
UIActionSheet *actionSheet = [[UIActionSheet alloc]
                              initWithTitle:@"Clear History?"
                              delegate:self
                              cancelButtonTitle:@"Cancel"
                              destructiveButtonTitle:@"OK"
                              otherButtonTitles:nil];


// use the same style as the nav bar
actionSheet.actionSheetStyle = self.navigationController.navigationBar.barStyle;    

[actionSheet showInView:self.view];
    [actionSheet release];


    }
如果选择“确定”,请执行以下操作:

  - (void)actionSheet:(UIActionSheet *)actionSheet

 didDismissWithButtonIndex:(NSInteger) buttonIndex
  {
if (!buttonIndex == [actionSheet cancelButtonIndex])
{

    //do what i want here!
}
  }

在头文件中,UIActionSheetDelegate包含在@interface中。

我不知道它是否能解决此问题,但您需要执行buttonIndex!=[actionSheet cancelButtonIndex]检查不相等而不是!buttonIndex==[actionSheet cancelButtonIndex]反转buttonIndex!检查是否相等。

您需要使用showFromBarButtonItem而不是showInView。问题是“取消”按钮被另一个视图(可能是工具栏或选项卡栏)覆盖,因此它无法获得您认为应该获得的触摸事件。有时还应使用showFromTabBar和showFromToolbar。请参见UIActionSheet类参考。

稍后,可能的解释是:

似乎是tabbar的问题。如果你给UIActionSheet打电话

从UITabViewController的子视图控制器,然后 取消按钮的点击测试在该部分失败 UIActionSheet,位于选项卡栏视图上方

如果改为传入UIAbbarController的视图,则UIActionSheet将按预期操作

[sheet showInView:self.parentViewController.tabBarController.view];
这里有更详细的解释:

[sheet showInView:self.parentViewController.tabBarController.view];