2011-03-27 42 views
10

我有一种方法称为-showMoreTools:它是:iPad的UIActionSheet表示多次

- (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的编程新手!

回答

33

为了消除它,当你点击按钮两次,你需要保持目前的轨道显示ActionSheet。我们在iPad应用中执行此操作,效果很好。

在你的类具有showMoreTools,在头放:

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

在类文件中,将其更改为:

-(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; 
} 
+0

解决方案适用于我,当我遇到同样的问题时,谢谢。我想知道为什么苹果没有提到这一点,因为他们明确地做到了这一点,当它“将拥有按钮的工具栏添加到popover的passthrough视图列表” – Bryan 2011-12-21 20:32:42

+0

另外,我认为你应该已经设置actionSheet_为null在actionSheet clickedButtonAtIndex: – Bryan 2011-12-21 20:40:57

+0

你不需要。如果您选择一个按钮,则调用clickedButtonAtIndex,然后调用didDismissWithButtonIndex。把这个= nil只需要那个方法。我只是在调试器中检查了它。 – christophercotton 2011-12-21 20:48:18

0

使用

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

我试过了,但它仍然显示了另一种。 – Francesc 2011-03-27 11:57:13

0

试图通过设置标志(是/否)

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

你的意思是如果actionsheet是可见的设置是的。点击另一个按钮检查动作表是否可见不正确... – 2011-04-07 14:15:01

1

我发现了另一个解决这个。问题是,当使用showFromBarButtonItem时,工具栏视图会自动添加到弹窗视图的弹窗列表中。当直接使用UIPopoverController时,您可以修改(并清除)直通视图,但是当它作为UIActionSheet的一部分显示时,您可以修改(并清除)直通视图。

无论如何,通过使用showFromRect,不存在弹出窗口可以自动添加到其传递视图的工具栏。所以,如果你知道这个(近似)矩形在您的按钮栏,你可以使用类似:

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

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

+1

这种方法的一个问题是,如果你支持旋转,你负责管理旋转后的弹出窗口的位置。 – 2013-03-28 23:32:18

0

我的方法类似于christophercotton's。

在我showActionSheet我检查actionsheet是可见的,而不是实例:

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

    //actionsheet code 
}