不要在iOS中的对话区域外用敲击手势解雇uialertcontroller表

时间:2017-04-04 05:54:44

标签: ios swift swift3

在我的iOS swift 3.0应用程序中,我在当前的ViewController上提供了UIAlertController工作表实例。但是当我在工作表区域外面(暗淡的半透明背景)点击时,我不想忽略那张纸,因为我已经取消了一个动作。 有什么想法吗?

我有更多按钮的MGSwipeTableViewCell。当用户点击那个"更多"按钮,代码执行。

func onClickMore(for vmCell: VmCell) {
    let sheet = UIAlertController(title: vmCell.vmItem?.vmNameWithoutIp, message: vmCell.vmItem?.ipAddress, preferredStyle: .actionSheet)

    sheet.addAction(UIAlertAction(title: "Create Ticket", style: .default) { (action: UIAlertAction) in

    })

    sheet.addAction(UIAlertAction(title: "Start VM", style: .default) { (action: UIAlertAction) in

    })

    sheet.addAction(UIAlertAction(title: "Restart VM", style: .default) { (action: UIAlertAction) in

    })

    sheet.addAction(UIAlertAction(title: "Stop VM", style: .destructive) { (action: UIAlertAction) in

    })

    sheet.addAction(UIAlertAction(title: "Cancel", style: .cancel) { (action: UIAlertAction) in

    })

    present(sheet, animated: true) { 
        sheet.view.superview?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: nil))
    }
}

1 个答案:

答案 0 :(得分:4)

UIAlertController的

键入警告

您可以下载sample project

将手势识别器添加到alertController superview 以处理用户交互

self.present(alertController, animated: true, completion: {() -> Void in
 alertController.view.superview?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: nil)
})

关于那个动作什么都不做

<强>更新

let alertController = UIAlertController(title: "Do something", message: "With this", preferredStyle: .alert)
    alertController.addAction(UIAlertAction(title: "Done", style: .default) { action in
        // perhaps use action.title here
    })

    self.present(alertController, animated: true, completion: {() -> Void in


        alertController.view.superview?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: nil))
    })

表示UIAlertController输入为actionSheet

您可以下载sample project

你可以用两种方式做到这一点

选项1

 alert.view.superview.subviews[0] isUserInteractionEnabled = false

选项2

   alert.view.superview?.subviews[0].addGestureRecognizer(UITapGestureRecognizer(target: self, action: nil))

例如

   self.present(sheet, animated: true, completion: {() -> Void in
    //    sheet.view.superview?.subviews[0].isUserInteractionEnabled = false;
      sheet.view.superview?.subviews[0].addGestureRecognizer(UITapGestureRecognizer(target: self, action: nil))

    })

完整代码

  let sheet = UIAlertController(title: "karthik", message: "check with", preferredStyle: .actionSheet)

    sheet.addAction(UIAlertAction(title: "Create Ticket", style: .default) { (action: UIAlertAction) in

    })

    sheet.addAction(UIAlertAction(title: "Start VM", style: .default) { (action: UIAlertAction) in

    })

    sheet.addAction(UIAlertAction(title: "Restart VM", style: .default) { (action: UIAlertAction) in

    })

    sheet.addAction(UIAlertAction(title: "Stop VM", style: .destructive) { (action: UIAlertAction) in

    })

    sheet.addAction(UIAlertAction(title: "Cancel", style: .cancel) { (action: UIAlertAction) in

    })



    self.present(sheet, animated: true, completion: {() -> Void in
    //    sheet.view.superview?.subviews[0].isUserInteractionEnabled = false;
      sheet.view.superview?.subviews[0].addGestureRecognizer(UITapGestureRecognizer(target: self, action: nil))

    })