按取消按钮不会缩回

时间:2010-08-23 07:59:30

标签: iphone objective-c cocoa-touch ios

我正在实施一份行动表。当我按“确定”按钮时,执行这些操作,按“取消”返回。 “确定”按钮工作正常,但是当我按下“取消”按钮时,没有任何反应,它不会缩回或执行任何操作,只需在操作表视图中挂起即可。

以下是我的代码:

要在导航栏上创建按钮:

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

3 个答案:

答案 0 :(得分:1)

迟到了,可能的解释可能是:

  

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

[sheet showInView:self.view]
  

来自视图控制器,它是UITabViewController的子视图   取消按钮上的命中测试在该部分中失败   位于标签栏视图上方的UIActionSheet。

如果您改为传入UITabBarController的视图,那么UIActionSheet将按预期运行。

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

这里有更详细的解释: UIActionSheet cancel button strange behaviour

答案 1 :(得分:0)

我不知道它是否能解决这个问题,但你想做buttonIndex != [actionSheet cancelButtonIndex](检查不等于)而不是!buttonIndex == [actionSheet cancelButtonIndex](反转buttonIndex(!)并检查是否相等)。

答案 2 :(得分:0)

您需要使用showFromBarButtonItem而不是showInView。问题是取消按钮被另一个视图覆盖 - 可能是工具栏或标签栏 - 所以它没有得到你认为它应该得到的触摸事件。有时也应该使用showFromTabBar和showFromToolbar(参见UIActionSheet类引用)。

相关问题