【问题标题】:app crashes when trying to change Message color of UIAlertController,swift尝试更改 UIAlertController、swift 的消息颜色时应用程序崩溃
【发布时间】: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


【解决方案1】:

UIAlertController 类中没有可用的键名属性是message,在这个地方使用attributedMessage 来更改消息。

在这个地方

   let alertController = UIAlertController.init(title: "Submit score", message: mess, preferredStyle: .alert)
  alertController.setValue(NSAttributedString(string: mess, attributes: [NSForegroundColorAttributeName: UIColor.red]), forKey: "message")

使用

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】:
     @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")
    
    }
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-11-09
        • 2015-10-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多