gpt4 book ai didi

ios - UIAlertcontroller 设置属性消息

转载 作者:IT王子 更新时间:2023-10-29 05:25:19 24 4
gpt4 key购买 nike

我们如何将 UIAlertcontroller 中的属性文本设置为消息。我的尝试代码如下所示,但它会使应用程序崩溃。

// create Attributed text

let myAttribute = [NSForegroundColorAttributeName: UIColor(red:122.0/255, green:125.0/255, blue:131.0/255, alpha:1.0),NSFontAttributeName: Constant.flinntRegularFont(15)]
let myAttribute2 = [NSForegroundColorAttributeName: UIColor.blackColor(),NSFontAttributeName: Constant.flinntMediumFont(15)]

let myString = NSMutableAttributedString(string: "You have been unsubscribed from ", attributes: myAttribute)
let myString2 = NSMutableAttributedString(string: self.course.course_name, attributes: myAttribute2)
let myString3 = NSMutableAttributedString(string: "\n\nYour refund will be initiated within one week.", attributes: myAttribute)
let myString4 = NSMutableAttributedString(string: "\n\nFor any help call us on", attributes: myAttribute)
let myString5 = NSMutableAttributedString(string: " 079-4014 9800", attributes: myAttribute2)
let myString6 = NSMutableAttributedString(string: " between 9:30 am to 6:30 pm on Monday to Saturday.\n\nWe will be always here with great deals to share.", attributes: myAttribute)

myString.appendAttributedString(myString2)
myString.appendAttributedString(myString3)
myString.appendAttributedString(myString4)
myString.appendAttributedString(myString5)
myString.appendAttributedString(myString6)

当前 UIAlertcontroller 代码

let alert = UIAlertController(title: "", message: "Select course", preferredStyle: UIAlertControllerStyle.Alert)
alert.setValue(myAttribute, forKey: "attributedMessage") // this line make a crash.

alert.addAction(UIAlertAction(title: "OK", style: .Cancel, handler: { (action) in
self.delegate?.courseRefundViewControllerCoursrRefunded?(self)
self.navigationController?.popViewControllerAnimated(true)
}))
self.presentViewController(alert, animated: true, completion: nil)

谢谢。

enter image description here

最佳答案

您的应用程序因数据类型不匹配而崩溃。

alert.setValue(<value>, forKey: "attributedMessage")

在这里<value>必须是 NSMutableAttributedString 的实例.

但是你传递的是 myAttribute,它是 Dictionary .

它正在尝试调用 length方法,但未在 Dictionary 上找到这就是应用程序崩溃的原因。

试试这个:

alert.setValue(myString, forKey: "attributedMessage")

关于ios - UIAlertcontroller 设置属性消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44130347/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com