【问题标题】:UIActionSheet Dismiss when click/ tap outsideUIActionSheet 点击/点击外部时关闭
【发布时间】:2011-09-13 13:19:21
【问题描述】:

我是 iPhone 开发的新手。谁能告诉我当我在 UIActionSheet 控件外面点击时如何关闭它?

在我的 actionSheet 中,我只有 datePicker 控件,现在它会在标签栏控件上弹出我想要的,每当用户点击它外部时,它应该关闭而不是使用 actionSheet 的取消按钮。 问候

【问题讨论】:

  • UIActionSheet:UIView。我希望你不能把触摸控制器放在外面
  • 现在这似乎是 iOS7 中的默认行为。
  • 这不是 iOS 7 iPhone 上的默认行为,仅适用于 iPad

标签: iphone objective-c uiactionsheet


【解决方案1】:

更简单的解决方案是添加一个取消按钮。这将自动启用点击背景以关闭:(Swift)

alertController.addAction(UIAlertAction(title: "Cancel",
                                        style: .cancel,
                                        handler: nil))

【讨论】:

    【解决方案2】:

    试试这个魔法:

        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOut:)];
        tap.cancelsTouchesInView = NO;
        [actionSheet.window addGestureRecognizer:tap];
        [tap release];
    

    还有这个方法:

    -(void)tapOut:(UIGestureRecognizer *)gestureRecognizer {
        CGPoint p = [gestureRecognizer locationInView:self.actionSheet];
        if (p.y < 0) {
            [self actionPickerDone:nil];
        }
    }
    

    【讨论】:

      【解决方案3】:
      【解决方案4】:

      SwiftUI 中,使用ActionSheet 时添加Alert.Button.Cancel 即可达到预期效果:

      @State var isShowingActionSheet: Bool = false
      @State var isShowingCamera: Bool = false
      
      .actionSheet(isPresented: $isShowingActionSheet) {
          ActionSheet(
              title: Text("Send an image"),
              buttons: [
                  .default(Text("Camera")) {
                      isShowingCamera = true
                  },
                  // This enables the "tap anywhere outside of the action sheet to dismiss it" behavior 
                  .cancel()
              ]
          )
      }
      

      【讨论】:

        【解决方案5】:

        添加一个带有按钮的工具栏来sismiss datePicker 和 actionSheet

        toolbarPicker = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
                toolbarPicker.barStyle=UIBarStyleBlackOpaque;
                [toolbarPicker sizeToFit];
                NSMutableArray *itemsBar = [[NSMutableArray alloc] init];
                //calls DoneClicked
                UIBarButtonItem *bbitem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(DoneClicked)];
                [itemsBar addObject:bbitem];
                [toolbarPicker setItems:itemsBar animated:YES];
        
                [menu addSubview:toolbarPicker];
        

        并为完成按钮添加这些操作

        -(BOOL)closeDatePicker:(id)sender{
        [menu dismissWithClickedButtonIndex:0 animated:YES];
        [txtDate resignFirstResponder];
        
        return YES;
        

        }

        //点击完成按钮时的动作 -(IBAction)DoneClicked{ [自我关闭日期选择器:自我]; menu.frame=CGRectMake(0, 44, 320, 416);

        }

        【讨论】:

          【解决方案6】:

          我建议您创建一个透明按钮并使其与您的视图大小相同,然后将其发送回去。创建一个方法来关闭操作表并将其与您的大按钮挂钩。我从未尝试过,但我认为值得尝试,因为如果可行,您可以将其设置为视图上的通用方法,以在用户点击文本字段(根据 HIG)和 UIActionSheet 之外时关闭键盘。

          但是我不知道以与弹出框相同的方式关闭 UIActionSheet 是否合适。无论如何,操作表都不需要一些输入吗?不然苹果为什么给你提供取消按钮?

          【讨论】:

            【解决方案7】:

            根据 IOS 6,您可以通过以下方式实现它:

            1. 检查 buttonIndex(只有在 buttonIndex>=0 时才运行你的东西)。点击外部操作表将使 buttonindex = -1。
            2. 实现 didDismissWithButtonIndex

            请注意,这仅在您实现 UIActionSheetDelegate 后有效。

            - (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
                NSLog(@"action sheet was dismissed");
            }
            
            - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
            {
                //if user click outside the action sheet, button index will be -1
                if(buttonIndex>0)
                {
                    NSString *buttonTitle = [actionSheet buttonTitleAtIndex:buttonIndex];
                }
            }
            

            【讨论】:

            • 即使在 iOS6 中,如果在操作表外单击,也不会使用 -1 的按钮索引调用委托。文档提到,如果取消按钮索引未设置,则将传递 -1 的按钮索引。
            猜你喜欢
            • 1970-01-01
            • 2011-03-22
            • 1970-01-01
            • 1970-01-01
            • 2018-12-13
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多