【问题标题】:Change UIAlertController message or title during its presentation在演示期间更改 UIAlertController 消息或标题
【发布时间】:2016-10-28 21:06:16
【问题描述】:

UIAlertController 出现时,是否可以更改UIAlertController 的标题或消息文本。

例如,当用户按下带有此消息的按钮时,我会发出警报:

“等待兑换码”

然后我使用 Alamofire 发出请求并获取代码,在我拥有它之后我想从警报中更改消息,而不是再次显示它,例如新的消息文本就是它:

"您的兑换码是:########"

更新

这是我的代码:

@IBAction func offerAction(_ sender: UIButton) {
        var code: String = "Generating códe"
        let message: String = "El código para redimir esta oferta es: \(code)"

        let alert = UIAlertController(title: nil, message: message, preferredStyle: .alert)
        let okAction = UIAlertAction(title: "Accept", style: .default, handler: nil)
        let redirect = UIAlertAction(title: "Website", style: .default) { (_) in
            // TODO open url web
        }

        if offer.redeemOfferOnline == .yes {
            alert.addAction(redirect)
        }
        alert.addAction(okAction)
        present(alert, animated: true, completion: nil)

        offer.code.getRedeemCode(id: offer.id) { (success, data) in
            if success {
                code = data
                alert.message = message

                // TODO end the code of changing the message of alert
            }
        }
}

【问题讨论】:

  • 是的,只需更新 titlemessage 属性。
  • @Paulw11 我在 Alamofire 请求的 callback 中尝试过,但没有任何反应...
  • 我可以获取 MainQueue 并更新此属性吗?
  • @Santiagocarmonagonzalez 你确定到达那行代码吗?
  • @alexburtnik 我完全确定,因为我使用断点还在控制台中打印我从请求中得到的代码

标签: ios swift uialertcontroller


【解决方案1】:

这当然是可能的,请检查以下内容:

class MyViewController:  UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let alertController = UIAlertController(title: "My Title", message: "My Message", preferredStyle: .alert)

        present(alertController, animated: true, completion: nil)

        // Do your queries and get your new title and then set the new title
        alertController.title = "Your new title"
    }

}

【讨论】:

  • 从用户体验的角度来看,这不是一个好主意。因为用户正在看到某些东西,现在突然看到其他东西而没有任何动画/反馈。用户会傻眼吗?!刚刚变了吗?!!最好关闭 alertView 并显示一个新的
  • 你真的认为 UX 明智的是最好关闭它并在此之后立即显示警报视图?我非常怀疑:)
  • 查看我的答案,了解如何为更改设置动画。
【解决方案2】:

可以更改标题和/或文本并对更改进行动画处理,因此:

[UIView transitionWithView: alertController.view
                  duration: 0.3
                   options: UIViewAnimationOptionTransitionCrossDissolve
                animations: ^(void) {
                    alertController.message = newMessage;
                }
                completion: nil];

或者在 Swift 中:

UIView.transition(with: alertController.view,
                  duration: 0.3,
                  options: .transitionCrossDissolve,
                  animations: { alertController.message = newMessage }
)

【讨论】:

    【解决方案3】:

    只需将您的警报控制器定义为:

    var controller:UIAlertController?
    

    然后像这样初始化你的警报控制器:

      controller = UIAlertController(title: "Title", message: "Yo", preferredStyle: .alert)
      self.present(controller!, animated: true, completion: nil)
    

    现在当你从服务器获取数据时,调用这个:

    self.controller?.title = "New Title"
    

    【讨论】:

      【解决方案4】:

      displayMyAlertMessage(userMessage: "Keshav Gera");

      func displayMyAlertMessage(userMessage: String)
          {
              let alert = UIAlertController(title: "Success", message: userMessage, preferredStyle: .alert)
      
              let action = UIAlertAction(title: "OK", style: .default, handler: nil)
      
              let imgTitle = UIImage(named:"imgTitle.png")
              let imgViewTitle = UIImageView(frame: CGRect(x: 10, y: 10, width: 30, height: 30))
              imgViewTitle.image = imgTitle
      
              alert.view.addSubview(imgViewTitle)
              alert.addAction(action)
      
              self.present(alert, animated: true, completion: nil)
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多