ios 防止 UIAlertController 关闭

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28919670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 05:08:43  来源:igfitidea点击:

Prevent UIAlertController to dismiss

iosuialertcontroller

提问by soulshined

I would like to prevent the UIAlertControllerfrom dismissing.

我想阻止UIAlertController解雇。

I have a UIAlertActionthat 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简单地将一个字符串附加到 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]];
    }];

I've also tried setting no to pasteMessage by:

我还尝试通过以下方式将 no 设置为 pasteMessage:

 [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;
       }
 }


Edit, i'm not looking to prevent the tapping of UIAlertActioni'm looking to prevent the UIAlertControllerfrom 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 UITextFieldby pressing an action (hence the reason I don't want it to be dismissed)

编辑,我不是要防止点击UIAlertAction我希望防止UIAlertController在点击所述操作时解除。该动作可以被启用/禁用,但我的目标是UITextField通过按下一个动作简单地将复制的消息粘贴到 中(因此我不希望它被解除)

I also realize setting the BOOL to dismissViewControllerAnimated:simply sets it to notanimate 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 newUIAlertControllerwhen selecting pasteMessage auto-populating the newUIAlertControllerstextField with the copied message, it works, but I feel like it's too hacky for what could be done.

我也意识到将 BOOL 设置为dismissViewControllerAnimated:简单地将其设置为为视图控制器解雇设置动画,我不希望它暗示它是为了停止实际解雇过程。简单地提供我尝试过的与我的目标相关的东西。我也试过在选择 pasteMessage 时展示一个新的UIAlertController,用复制的消息自动填充新的UIAlertControllerstextField,它有效,但我觉得它对于可以做的事情来说太笨拙了。

采纳答案by Aaron

EDIT:

编辑:

Updated for Swift 3

为 Swift 3 更新

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

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

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

首先,创建延迟加载计算的变量,你UIAlertControllerUIAlertAction你想从触发您的视图控制器上,因此,为了防止他们在用手势识别器的选择方法是访问(self在选择影射这一切是一个视图控制器内)。

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)
}()

Make sure your gesture recognizer above is a UILongPressGestureRecognizerset 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:

确保上面的手势识别器是一个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
    }
}

Then you just present the UIAlertControllerwherever.

然后,您只需呈现UIAlertControllerwhere 即可。

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

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 UIAlertControllerso the user can trigger that method if they tap anywhere on the alert (besides the cancel button).

这显然是一个 hack,但我知道没有其他方法可以在没有 hack 的情况下实现这一点,因为它不是要实现的。例如,手势识别器与 相关联,UIAlertController因此如果用户点击警报上的任何位置(取消按钮除外),他们就可以触发该方法。

ORIGINAL ANSWER:

原始答案:

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

这是尽可能接近我可以解决的问题。如果有某种方法可以将解雇过渡时间自定义为无,那么您可以将其设置animated:为 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, 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)
    }
}

I'm only familiar with swift

我只熟悉 swift

回答by Nikita Ivaniushchenko

Pretty much the same question is answered here

在这里回答几乎相同的问题

Text field on alert supports paste option, so there is no real reason to have separate button on alert to indicate "paste" option.

警报上的文本字段支持粘贴选项,因此没有真正的理由在警报上有单独的按钮来指示“粘贴”选项。

Otherwise you should mimic UIAlertController and reimplement it with desiread behavior.

否则你应该模仿 UIAlertController 并用想要的行为重新实现它。