防止UIAlertController解雇 [英] Prevent UIAlertController to dismiss

查看:112
本文介绍了防止UIAlertController解雇的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想阻止 UIAlertController 解散。

我有一个 UIAlertAction ,它只是将一个字符串附加到UIAlertTextField中,但是,一旦点击它就会解散视图控制器[不希望的。我已经尝试添加一个不良结果的NSNotification。

I have a UIAlertAction that simply appends a string into the UIAlertTextField, however, once tapped it dismisses the view controller [undesired]. I've tried adding an NSNotification with undesired results.

    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设置为:* /

$ b

I've also tried setting no to pasteMessage by:

 [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 中(因此我不希望它的原因)被解雇)


Edit, i'm not looking to prevent the tapping of UIAlertAction i'm looking to prevent the UIAlertController from dismissing when tapping on said action. The action can be enabled/disabled whatever, but my goal is to simply paste the copied message into the UITextField by pressing an action (hence the reason I don't want it to be dismissed)

我也意识到将BOOL设置为 dismissViewControllerAnimated:只需将其设置为而不是动画视图控制器被解雇,我不希望它暗示它是为了停止实际的解雇过程。简单地提供我尝试过与我的目标相关的事情。我还尝试在选择pasteMessage时自动填充 new ,然后展示 UIAlertController UIAlertControllers 带有复制消息的textField,它可以正常工作,但我觉得这样做太烂了。

I also realize setting the BOOL to dismissViewControllerAnimated: simply sets it to not animate the view controllers dismissal, I don't want it to imply it was for stopping the actual dismissal process. Simply offering the things I've tried in relation to my goal. I've also tried presenting a new UIAlertController when selecting pasteMessage auto-populating the new UIAlertControllers textField with the copied message, it works, but I feel like it's too hacky for what could be done.

推荐答案

编辑:

更新为Swift 3

所以我真的让这个工作了。简而言之,它涉及在解雇发生之前触发的 UIAlertController 添加手势识别器。

So I actually got this to work. In short, it involves adding a gesture recognizer to the UIAlertController that triggers before the dismissal occurs.

首先,你为 UIAlertController 创建延迟加载的计算变量,并阻止在视图控制器上触发 UIAlertAction 以便它们'可以通过手势识别器的选择器方法访问(选择器中的 self ,暗示所有这些都在视图控制器中)。

First, you create lazily loaded computed variables for your UIAlertController and the UIAlertAction you want to prevent from triggering on your view controller so that they're accessible by the gesture recognizer's selector method (self in the selector insinuates that all of this is inside a view controller).

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 ,实现你的自定义代码,并在手势完成后重新启用动作(用户已经修改)。见下文:

Make sure your gesture recognizer above is a UILongPressGestureRecognizer set with a minimum press duration of 0. That way you can access the state of the gesture (for when the user touches down) before the action is triggered fully. There you can disable the UIAlertAction, implement your custom code, and reenable the action after the gesture has completed (user has touched up). See below:

@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 ,因此如果用户点击警报上的任何位置(除了取消按钮),用户就可以触发该方法。

This is obviously a hack but there's no other way that I know of to achieve this without a hack since it's not meant to be achieved. For example, the gesture recognizer is tied to the UIAlertController so the user can trigger that method if they tap anywhere on the alert (besides the cancel button).

原始答案:

这就像我可以接近黑客一样。如果有某种方法可以将解雇转换时间自定义为空,那么您可以将动画:设置为false并且它看起来像是相同的警报,但我不认为它是可能

This is as close as I could come to a hackaround. If there was some way to customize the dismissal transition time to nothing then you could set animated: to false and it would look like the same alert, but I don't think it's possible

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, animated: true, completion: nil)
        }

        let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)

        alert.addAction(appendAction)
        alert.addAction(cancelAction)

        self.presentViewController(alert, animated: true, completion: nil)
    }
}

我只熟悉swift

这篇关于防止UIAlertController解雇的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆