防止UIAlertControllerclosures

我想阻止UIAlertController被解雇。

我有一个UIAlertAction ,只是将一个string追加到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来通过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; } } 

编辑,我不想阻止UIAlertAction我正在寻找,以防止UIAlertController解雇时,说上述行动。 这个动作可以被启用/禁用,但是我的目标是通过按一个动作(因此我不希望它被解雇)将复制的消息粘贴到UITextField

我也意识到设置BOOL dismissViewControllerAnimated:简单地设置它animation的视图控制器解雇,我不希望它暗示它是停止实际解雇过程。 简单地提供我已经尝试过的与我的目标有关的事情。 我也尝试提出一个新的 UIAlertController当selectpasteMessage自动填充新的 UIAlertControllers与复制的消息,它的工作原理,但我觉得这是太hacky可以做什么。

编辑:

更新了Swift 3

所以我实际上得到了这个工作。 简而言之,它涉及在解除发生之前触发的UIAlertController添加手势识别器。

首先,为UIAlertControllerUIAlertAction创build延迟加载的计算variables,以防止在视图控制器上触发,以便通过手势识别器的select器方法访问这些variables(select器中的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) }() 

请确保您的手势识别器是一个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所以用户可以触发该方法,如果他们点击提醒的任何地方(除了取消button)。

原文答案:

这是尽可能接近我可以来hackaround。 如果有一种方法可以将解雇过渡时间定义为无,那么可以将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) } } 

我只是很快熟悉

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

警报上的文本字段支持粘贴选项,所以没有真正的理由有单独的警报button来指示“粘贴”选项。

否则,你应该模仿UIAlertController并重新实现与愿望的行为。