在呈现期间更改 UIAlertController 消息或标题
Change UIAlertController message or title during its presentation
可以在 ViewController
显示时更改 UIAlertController
的标题或消息文本。
例如,当用户按下带有此消息的按钮时,我会发出警报:
"Waiting for redeem code"
然后我使用 Alamofire 发出请求并获取代码,在我得到它之后我想将消息从警报中更改而不是再次关闭并再次呈现它,例如新消息正文是:
"Your redeem code is : ########"
已更新
这是我的代码:
@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
}
}
}
当然可以,请检查以下内容:
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"
}
}
只需将您的 alertcontroller 定义为:
var controller:UIAlertController?
然后像这样初始化您的警报控制器:
controller = UIAlertController(title: "Title", message: "Yo", preferredStyle: .alert)
self.present(controller!, animated: true, completion: nil)
现在当你从服务器获取数据时,调用这个:
self.controller?.title = "New Title"
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)
}
可以更改标题 and/or 文本并为更改设置动画,因此:
[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 }
)
可以在 ViewController
显示时更改 UIAlertController
的标题或消息文本。
例如,当用户按下带有此消息的按钮时,我会发出警报:
"Waiting for redeem code"
然后我使用 Alamofire 发出请求并获取代码,在我得到它之后我想将消息从警报中更改而不是再次关闭并再次呈现它,例如新消息正文是:
"Your redeem code is : ########"
已更新
这是我的代码:
@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
}
}
}
当然可以,请检查以下内容:
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"
}
}
只需将您的 alertcontroller 定义为:
var controller:UIAlertController?
然后像这样初始化您的警报控制器:
controller = UIAlertController(title: "Title", message: "Yo", preferredStyle: .alert)
self.present(controller!, animated: true, completion: nil)
现在当你从服务器获取数据时,调用这个:
self.controller?.title = "New Title"
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)
}
可以更改标题 and/or 文本并为更改设置动画,因此:
[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 }
)