首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >当试图更改UIAlertController的消息颜色时,应用程序崩溃

当试图更改UIAlertController的消息颜色时,应用程序崩溃
EN

Stack Overflow用户
提问于 2017-02-22 12:30:40
回答 3查看 689关注 0票数 1

当试图在UIAlertController中更改文本的颜色时,我会得到下面描述的错误

由于未识别的异常“NSInvalidArgumentException”终止应用程序,原因:'-NSConcreteAttributedString rangeOfCharacterFromSet::未识别的选择器发送到实例0x60000003e320‘

函数,该函数试图更改颜色:

代码语言:javascript
运行
复制
@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-属性,所以我不理解错误。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-02-22 12:46:56

message类中没有可用的密钥名为UIAlertController的属性,在这里使用attributedMessage来更改消息。

在这个地方

代码语言:javascript
运行
复制
   let alertController = UIAlertController.init(title: "Submit score", message: mess, preferredStyle: .alert)
  alertController.setValue(NSAttributedString(string: mess, attributes: [NSForegroundColorAttributeName: UIColor.red]), forKey: "message")

使用

代码语言:javascript
运行
复制
let alertController = UIAlertController.init(title: "Submit score", message: mess, preferredStyle: .alert)
alertController.setValue(NSAttributedString(string: mess, attributes: [NSForegroundColorAttributeName: UIColor.red]), forKey: "attributedMessage")

如果您想更改UIAlertController的标题,那么您应该使用'attributedTitle‘键。

你得到的输出

票数 2
EN

Stack Overflow用户

发布于 2017-02-22 12:39:50

代码语言:javascript
运行
复制
 @objc private func submitScore(color: Bool = false) {
    var mess = ""
    mess = color ? "Invalid username" : ""
    let alertController = UIAlertController.init(title: "Submit score", message: mess, preferredStyle: .alert)

    //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")
    })

    alertController.setValue(NSAttributedString(string: mess, attributes: [NSForegroundColorAttributeName: UIColor.red]), forKey: "message")

}
票数 0
EN

Stack Overflow用户

发布于 2017-02-22 12:41:03

您似乎试图将NSAttributedString设置为字符串变量message。试试https://stackoverflow.com/a/42118706/7064692

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

https://stackoverflow.com/questions/42391568

复制
相关文章

相似问题

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