ios – 防止UIAlertController关闭

ios – 防止UIAlertController关闭,第1张

概述我想防止UIAlertController被解雇. 我有一个UIAlertAction,只需将一个字符串附加到UIAlertTextField中,但是,一旦轻击它将关闭视图控制器[不需要].我尝试添加一个NSNotification与不合要求的结果. UIAlertAction *pasteMessage = [UIAlertAction actionWithTitle:@"Paste Messa 我想防止UIAlertController被解雇.

我有一个UIAlertAction,只需将一个字符串附加到UIAlertTextFIEld中,但是,一旦轻击它将关闭视图控制器[不需要].我尝试添加一个NSNotification与不合要求的结果.

UIAlertAction *pastemessage = [UIAlertAction actionWithTitle:@"Paste Message" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {        UITextFIEld *textFIEld = alertC.textFIElds.firstObject;        textFIEld.text = [textFIEld.text stringByAppendingString:[Nsstring stringWithFormat:@"%@",copIEdString]];    }];

我也试过设置no粘贴消息:

[alertC canPerformAction:@selector(dismissVIEwControllerAnimated:completion:) withSender:pastemessage];-(voID)dismissVIEwControllerAnimated:(BOol)flag completion:(voID (^)(voID))completion {    UIAlertController *alertController = (UIAlertController *)self.presentedVIEwController;    UIAlertAction *paste = alertController.actions.firstObject;       if (paste) {         flag = NO;       } else {         flag = YES;       } }

编辑,我不是想阻止UIAlertAction的攻击我想防止UIAlertController在点击所述动作时被解雇.该 *** 作可以启用/禁用任何,但我的目标是通过按一个动作(因此我不想要它被解雇的原因)将复制的邮件简单地粘贴到UITextFIEld中.

我也意识到设置BOol以关闭VIEwControllerAnimated:只需将其设置为不动态视图控制器解雇,我不希望它暗示是为了停止实际的解雇过程.只是提供我所尝试的与我的目标相关的事情.在选择粘贴消息的情况下,我还尝试介绍一个新的UIAlertController,它使用复制的消息自动填充新的UIAlertControllers textFIEld,但它的工作原理很好,但是我觉得这样做太麻烦了.

解决方法 编辑:

更新为Swift 3

所以我实际上得到了这个工作.简而言之,它包括在UIAlertController中添加一个手势识别器,触发器在解除发生之前触发.

首先,为您的UIAlertController和您希望防止在视图控制器上触发的UIAlertAction创建延迟加载的计算变量,以便它们可以通过手势识别器的选择器方法访问(选择器中的自己隐藏所有这些都在视图内)控制器).

lazy var alert: UIAlertController = {    let alert = UIAlertController(Title: "Title",message: "Message",preferredStyle: .alert)    alert.addTextFIEld(configurationHandler: nil)    let appendAction = self.appendAction    let cancelAction = UIAlertAction(Title: "Cancel",style: .cancel,handler: nil)    alert.addAction(appendAction)    alert.addAction(cancelAction)    let gestureRecognizer = UILongPressGestureRecognizer(target: self,action: #selector(self.append(sender:)))    gestureRecognizer.minimumPressDuration = 0.0    alert.vIEw.addGestureRecognizer(gestureRecognizer)    return alert}()lazy var appendAction: UIAlertAction = {    return UIAlertAction(Title: "Paste Message",style: .default,handler: nil)}()

确保上面的手势识别器是一个UILongPressGestureRecognizer设置,最小压制持续时间为0.这样,您可以在完全触发动作之前访问手势的状态(用户触摸时).在那里,您可以禁用UIAlertAction,实现您的自定义代码,并在手势完成(用户已触及)后重新启动该 *** 作.见下文:

@objc func append(sender: UILongPressGestureRecognizer) {    if sender.state == .began {        appendAction.isEnabled = false    } else if sender.state == .ended {        // Do whatever you want with the alert text fIElds        print(alert.textFIElds![0].text)        appendAction.isEnabled = true    }}

那么你只要在任何地方呈现UIAlertController.

@IBAction func showAlert(sender: AnyObject) {    self.present(alert,animated: true,completion: nil)}

这显然是一个黑客,但是没有别的方法,我知道如果没有一个黑客就可以实现这个目标,因为它不是要实现的.例如,手势识别器绑定到UIAlertController,以便用户可以触发该方法,如果他们点击警报上的任何地方(除了取消按钮).

原始答案:

这是尽可能接近我可以来一个破解.如果有一些方法可以将解雇过渡时间自定义为无效,那么您可以设置动画:为false,它看起来像同样的警报,但我不认为这是可能的

class VIEwController: UIVIEwController {    @IBAction func alert(sender: AnyObject) {        let alert = UIAlertController(Title: "Title",message: "message",preferredStyle: .Alert)        alert.addTextFIElDWithConfigurationHandler(nil)        let appendAction = UIAlertAction(Title: "Append text",style: .Default) { _ in            var textFIEld = alert.textFIElds![0] as UITextFIEld            // Append text here            self.presentVIEwController(alert,completion: nil)        }        let cancelAction = UIAlertAction(Title: "Cancel",style: .Cancel,handler: nil)        alert.addAction(appendAction)        alert.addAction(cancelAction)        self.presentVIEwController(alert,completion: nil)    }}

我只是熟悉swift

总结

以上是内存溢出为你收集整理的ios – 防止UIAlertController关闭全部内容,希望文章能够帮你解决ios – 防止UIAlertController关闭所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1113566.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-29
下一篇2022-05-29

发表评论

登录后才能评论

评论列表(0条)

    保存