防止UIAlertController解雇

sou*_*ned 31 ios uialertcontroller

我想阻止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]];
    }];
Run Code Online (Sandbox Code Playgroud)

我也尝试过将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;
       }
 }
Run Code Online (Sandbox Code Playgroud)

编辑,我不打算阻止攻击UIAlertAction我希望防止UIAlertController在点击上述行动时解雇.无论如何都可以启用/禁用该操作,但我的目标是UITextField通过按动作简单地将复制的消息粘贴到该动作中(因此我不希望它被解除)

我也意识到设置BOOL dismissViewControllerAnimated:只是将它设置为动画视图控制器解雇,我不希望它暗示它是为了停止实际的解雇过程.简单地提供我尝试过与我的目标相关的事情.我还尝试在选择pasteMessage时使用复制的消息自动填充新的 textField 时会出现一个新的 ,它可以工作,但我觉得它太可能无法完成.UIAlertController UIAlertControllers

Aar*_*ron 28

编辑:

针对Swift 3进行了更新

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

首先,为您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)
}()
Run Code Online (Sandbox Code Playgroud)

确保上面的手势识别器是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
    }
}
Run Code Online (Sandbox Code Playgroud)

然后你只要出示在UIAlertController哪里.

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

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

原始答案:

这就像我可以接近黑客一样.如果有某种方法可以将解雇转换时间自定义为空,那么您可以将其设置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)
    }
}
Run Code Online (Sandbox Code Playgroud)

我只熟悉swift

  • @Aaron对于那些发现这个答案不起作用的人,设置`gestureRecogniser`的委托并实现`gestureRecognizer(_:shouldRecognizeSimultaneouslyWith :))并在其中返回true ..请验证并更新你的答案 (6认同)