【发布时间】:2017-02-22 12:30:40
【问题描述】:
我在尝试更改 UIAlertController 内的文本颜色时遇到如下所述的错误:
由于未捕获的异常而终止应用 'NSInvalidArgumentException',原因:'-[NSConcreteAttributedString rangeOfCharacterFromSet:]: 无法识别的选择器发送到实例 0x60000003e320'
尝试改变颜色的函数:
@objc private func submitScore(color: Bool = false) {
var mess = ""
mess = color ? "Invalid username" : ""
let alertController = UIAlertController.init(title: "Submit score", message: mess, preferredStyle: .alert)
alertController.setValue(NSAttributedString(string: mess, attributes: [NSForegroundColorAttributeName: UIColor.red]), forKey: "message")
//Submit button calls function again
let submit = UIAlertAction(title: "Submit", style: .default) { (submit) in
self.submitScore(color: true)
}
let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: { (cancel) in
})
alertController.addTextField(configurationHandler: {(textfield) in
textfield.placeholder = "Nickname"
})
alertController.addAction(submit)
alertController.addAction(cancel)
present(alertController, animated: true, completion: {(alertController) in
print("shown")
})
}
如果我删除第 5 行,问题就解决了。NSForegroundColorAttributeName 是一个有效的 NSAttributedString-property,所以我不明白错误..
【问题讨论】:
-
属性
message的类型为String,因此您不能向其添加任何属性。没有用于设置属性消息的公开属性。
标签: ios swift xcode swift3 uialertcontroller