首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >防止UIAlertController解散

防止UIAlertController解散
EN

Stack Overflow用户
提问于 2015-03-07 20:31:53
回答 2查看 32.3K关注 0票数 41

我想防止UIAlertController被解雇。

我有一个UIAlertAction,它简单地将一个字符串附加到UIAlertTextField中,但是,一旦点击它,它就会解散视图控制器。我尝试添加一个结果不理想的NSNotification。

代码语言:javascript
复制
    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:

代码语言:javascript
复制
 [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:只是将其设置为不动画视图控制器的解雇,我不希望它暗示它是为了停止实际的解雇过程。简单地提供我尝试过的与我的目标相关的东西。我还试着在选择自动填充新的UIAlertControllers textField时,使用复制的消息来显示一个新的 UIAlertController,但我觉得这太麻烦了。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-03-10 05:06:01

编辑:为Swift 5更新

编辑:更新为包含@skywalker的反馈

所以我把这个弄到手了。简而言之,它包括向UIAlertController添加一个长按压手势识别器,在解雇发生之前触发。

首先,在视图控制器中为UIAlertController和要防止触发的UIAlertAction创建延迟加载的计算变量,以便可以通过要附加到警报的手势识别器的选择器方法访问self (选择器中的self暗示所有这些都在视图控制器中)。

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

代码语言:javascript
复制
@objc func append(sender: UILongPressGestureRecognizer) {
    
    switch sender.state {
    case .began:
        appendAction.isEnabled = false
    case .ended:
        // Do whatever you want with the alert text fields
        print(alert.textFields?[0].text)
        appendAction.isEnabled = true
    default:
        return
    }
}

此外,确保拥有此警报表示的视图控制器符合UIGestureRecognizerDelegate,以便识别同时的手势。

代码语言:javascript
复制
extension YourViewController: UIGestureRecognizerDelegate {

    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        return true
    }
}

然后,只要在任何地方显示UIAlertController即可。

代码语言:javascript
复制
func showAlert() {
    self.present(alert, animated: true, completion: nil)
}

这显然是一个黑客,但没有其他的方式,我知道,以实现这一点,没有黑客,因为它是不打算实现的。例如,手势识别器被绑定到UIAlertController,这样用户就可以触发该方法,如果他们点击警报的任何地方(除了cancel按钮)。

原始答案:

这是我最接近的一轮攻击。如果有某种方法将解雇转换时间自定义为空,那么您可以将animated:设置为false,并且它看起来类似于相同的警报,但我认为这是不可能的

代码语言:javascript
复制
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)
    }
}

我只熟悉斯威夫特

票数 35
EN

Stack Overflow用户

发布于 2015-03-16 22:22:50

here也回答了同样的问题

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

否则,您应该模仿UIAlertController,并使用desiread行为重新实现它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28919670

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档