我有一个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