UIActionSheet,如何一键关闭它?

发布于 2024-11-28 13:38:05 字数 715 浏览 1 评论 0原文

UIActionSheet,如何一键关闭它? 我必须单击一个按钮两次才能将其关闭,我该怎么做才能使其在第一次单击后可关闭?!这是我的代码:

-(void)buttonHeld:(id)sender
{



    UIActionSheet *popupQuery = [[UIActionSheet alloc] initWithTitle:@"Delete Contact?!" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Delete" otherButtonTitles: nil];

    popupQuery.actionSheetStyle = UIActionSheetStyleBlackOpaque;
    [popupQuery showInView:self.view];
    [popupQuery release];




}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {


  if (buttonIndex == 0) {

 NSLog(@"Delete button clicked Button Clicked");


    else if (buttonIndex == 1) {

   NSLog(@"Cancel Button Clicked");
   } 

}

UIActionSheet, how to dismiss it with a single click?
I have to click a button 2 times so it gets dismissed, what should I do to make it dismissble after the first click?! Here is my code:

-(void)buttonHeld:(id)sender
{



    UIActionSheet *popupQuery = [[UIActionSheet alloc] initWithTitle:@"Delete Contact?!" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Delete" otherButtonTitles: nil];

    popupQuery.actionSheetStyle = UIActionSheetStyleBlackOpaque;
    [popupQuery showInView:self.view];
    [popupQuery release];




}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {


  if (buttonIndex == 0) {

 NSLog(@"Delete button clicked Button Clicked");


    else if (buttonIndex == 1) {

   NSLog(@"Cancel Button Clicked");
   } 

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

私野 2024-12-05 13:38:06

根据您的方法“buttonHeld”的名称,我的假设是您正在尝试从 UILongPressGestureRecognizer 启动 UIActionSheet。如果您希望它按预期工作,您将需要使用下面列出的代码。该问题源于 UILongPressGestureRecognizer 是一种“连续手势”,会多次触发并经历几种不同的状态。您需要监视正确的状态,然后加载 UIActionSheet。我遇到了完全相同的问题,这就是为我解决的问题。

- (IBAction) buttonHeld:(UILongPressGestureRecognizer *)sender {
    if (sender.state == UIGestureRecognizerStateBegan) {
        UIActionSheet *popupQuery = [[UIActionSheet alloc] 
                                      initWithTitle:@"Delete Contact?!" 
                                           delegate:self 
                                  cancelButtonTitle:@"Cancel" 
                             destructiveButtonTitle:@"Delete" 
                                  otherButtonTitles:nil];

        popupQuery.actionSheetStyle = UIActionSheetStyleBlackOpaque;
        [popupQuery showInView:self.view];
    }
}

Based on the name of your method "buttonHeld", my assumption is that you are trying to launch the UIActionSheet from a UILongPressGestureRecognizer. You will need to use the code listed below if you want it to work as expected. The problem stems from UILongPressGestureRecognizer being a 'continuous gesture' which fires multiple times and progresses through several different states. You'll need to monitor for the correct state and then load your UIActionSheet. I had the exact same problem and this is what solved it for me.

- (IBAction) buttonHeld:(UILongPressGestureRecognizer *)sender {
    if (sender.state == UIGestureRecognizerStateBegan) {
        UIActionSheet *popupQuery = [[UIActionSheet alloc] 
                                      initWithTitle:@"Delete Contact?!" 
                                           delegate:self 
                                  cancelButtonTitle:@"Cancel" 
                             destructiveButtonTitle:@"Delete" 
                                  otherButtonTitles:nil];

        popupQuery.actionSheetStyle = UIActionSheetStyleBlackOpaque;
        [popupQuery showInView:self.view];
    }
}
Spring初心 2024-12-05 13:38:06

这就是我关闭操作表的方式...

  [actionSheet dismissWithClickedButtonIndex:0 animated:YES];

希望这有帮助

This is how i dismiss the actionsheet...

  [actionSheet dismissWithClickedButtonIndex:0 animated:YES];

Hope this helps

季末如歌 2024-12-05 13:38:06

我也点击了几下才将其取消。问题最终是我将它附加到哪个视图。

[action showInView:self.view];

self.view仅在IB中被引用。我将其更改为不同的视图并且有效。

It took me a few taps to get it to cancel as well. The problem ended up being which view I attached it to.

[action showInView:self.view];

self.view was only referenced in IB. I changed it to a different view and it worked.

奈何桥上唱咆哮 2024-12-05 13:38:06

当您分配操作表时,无需在此委托中声明取消按钮,默认情况下会

[[UIActionSheet alloc] initWithTitle:@"" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:btnTitle,@"Send for a date range",nil];

从任何其他事件中关闭操作表,您可以使用此方法

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

在您的代码中存在语法错误,您将 else 块放入其中如果阻止。

No need to declare in this delegate when you alloc the actionsheet you have make cancel Button that by default dismiss the ActionSheet some thing like this

[[UIActionSheet alloc] initWithTitle:@"" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:btnTitle,@"Send for a date range",nil];

from any other event you can use this method

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

In your code there is a syntax error you are putting else block in if block.

作妖 2024-12-05 13:38:06

如果您正在为 iPhone 进行开发,那么使用起来总是安全的:

[popupQuery showFromBarButtonItem:发送者动画:是]

或 showFromTabBar: 或 showFromToolBar 等而不是 showinView。

If u're developing for the iphone it's always safe to use :

[popupQuery showFromBarButtonItem:sender animated:YES]

or showFromTabBar: or showFromToolBar etc. instead of showinView.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文