【问题标题】:iPad's UIActionSheet showing multiple timesiPad 的 UIActionSheet 显示多次
【发布时间】:2011-03-27 11:42:27
【问题描述】:

我有一个名为 -showMoreTools: 的方法:

- (IBAction) showMoreTools:(id)sender {
    UIActionSheet *popupQuery = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"Close" otherButtonTitles:@"Add Bookmark", @"Add to Home Screen", @"Print", @"Share", nil];
    popupQuery.actionSheetStyle = UIActionSheetStyleDefault;
    popupQuery.dismiss
    [popupQuery showFromBarButtonItem:moreTools animated:YES];
    [popupQuery release];
}
当用户点击UIBarButtonItem 时,它会显示UIActionSheet,但是,如果用户想要在不点击关闭按钮的情况下关闭UIActionSheet,(点击UIBarButtonItem,那么它会显示UIActionSheet 在第一个UIActionSheet

有可能以某种方式再次录制UIBarButtonItem 以关闭UIActionSheet

非常感谢——我是 iOS 编程的新手!

【问题讨论】:

    标签: ipad uibarbuttonitem uiactionsheet


    【解决方案1】:

    为了在您单击按钮两次时将其关闭,您需要跟踪当前显示的 ActionSheet。我们在 iPad 应用程序中执行此操作,效果很好。

    在您的具有 showMoreTools 的类中,在标题中放置:

    @interface YourClassHere : NSObject <UIActionSheetDelegate> {
          UIActionSheet* actionSheet_;  // add this line
    }
    

    在class文件中,修改为:

    -(IBAction) showMoreTools:(id)sender {
        // currently displaying actionsheet?
        if (actionSheet_) {
            [actionSheet_ dismissWithClickedButtonIndex:-1 animated:YES];
            actionSheet_ = nil;
            return;
        }
    
        actionSheet_ = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"Close" otherButtonTitles:@"Add Bookmark", @"Add to Home Screen", @"Print", @"Share", nil];
        actionSheet_.actionSheetStyle = UIActionSheetStyleDefault;
        [popupQuery showFromBarButtonItem:moreTools animated:YES];
        [actionSheet_ release];  // yes, release it. we don't retain it and don't need to
    }
    
    
    - (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
        // just set to nil
        actionSheet_ = nil;
    }
    

    【讨论】:

    • 当我遇到同样的问题时,解决方案对我很有效,谢谢。我想知道为什么 Apple 不提及这一点,因为当它“将拥有按钮的工具栏添加到弹出窗口的直通视图列表中”时,他们明确地做到了这一点
    • 另外我认为你会在 actionSheet clickedButtonAtIndex 中将 actionSheet_ 设置为 nil:
    • 你不需要。如果您选择一个按钮,则会调用 clickedButtonAtIndex,然后还会调用 didDismissWithButtonIndex。仅在该一种方法中才需要放置 =nil 。我只是在调试器中仔细检查了它。
    • 碰巧碰到一个案例,其中 Bryan 是正确的。当我从UIActionSheet:clickedButtonAtIndex 弹出UIAlertView 时,didDismissWithButtonIndex 不会被调用。
    • 条件if (actionSheet_) 可以替换为if ([actionSheet_ superview])。我使用了来自DTFoundation 的 DTActionSheet,因此覆盖取消不适合我。
    【解决方案2】:

    我找到了另一个解决方案。问题是当使用showFromBarButtonItem 时,工具栏视图会自动添加到弹出框的直通视图列表中。您可以在直接使用 UIPopoverController 时修改(和清除)直通视图,但当它作为 UIActionSheet 的一部分呈现时则不能。

    无论如何,通过使用showFromRect,弹出框不会自动添加到其直通视图中的工具栏。因此,如果您知道按钮栏所在的(近似)矩形,则可以使用以下内容:

    CGRect buttonRect = CGRectIntersection(toolbar.frame, CGRectMake(0, 0, 60, self.frame.size.height));
    [popupQuery showFromRect:buttonRect inView:self animated:YES];
    

    在上面的示例中,我的按钮位于工具栏的左侧。

    【讨论】:

    • 这种方法的一个问题是,如果您支持旋转,您需要负责管理旋转后弹出框的位置。
    【解决方案3】:

    尝试设置标志(是/否)

    -(void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated
    

    【讨论】:

    • 你的意思是如果操作表是可见的设置是。单击另一个按钮时,检查动作表是否可见不正确...
    【解决方案4】:

    使用

    - (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated
    

    【讨论】:

      【解决方案5】:

      我的方法类似于 christophercotton 的。

      在我的 showActionSheet 中,我检查操作表是否可见而不是实例化:

      - (IBAction)showActionSheet:(id)sender
      {
          if ([self.fullActionSheet isVisible]) {
              [self.fullActionSheet dismissWithClickedButtonIndex:-1 animated:NO];
              _fullActionSheet = nil;
              return;
          }
      
          //actionsheet code
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多