gpt4 book ai didi

ios - 来自扩展的 UIAlertController

转载 作者:行者123 更新时间:2023-11-28 15:20:21 24 4
gpt4 key购买 nike

我有代码可以测试插入只有数字

如果我插入字母,如何使用消息进行提醒?

enter image description here

String+onlyNumbers.swift

extension String {
func onlyNumbers() ->String{
do{
let regex = try NSRegularExpression(pattern: "([//.,\\d])*", options:[.dotMatchesLineSeparators])
var result : String = ""

for resultMatch in regex.matches(in: self, options: NSRegularExpression.MatchingOptions.init(rawValue: 0), range: NSMakeRange(0, NSString(string: self).length)) {
result += NSString(string: self).substring(with: resultMatch.range)
}
return result
}
catch
{

}
return ""
}
}

ViewControler.swift

let saveAction = UIAlertAction(title: NSLocalizedString("Past",comment: ""), style: .default, handler: {
(alert: UIAlertAction!) -> Void in
self.displayResultLabel.text = UIPasteboard.general.string?.onlyNumbers()
print("Pasted")
})

"""""

let saveAction = UIAlertAction(title: NSLocalizedString("Past",comment: ""), style: .default, handler: {
(alert: UIAlertAction!) -> Void in
if UIPasteboard.general.string?.onlyNumbers() == "" {
let alertController = UIAlertController(title: "Calc Pro", message: UIPasteboard.general.string, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
self.present(alertController, animated: false, completion: nil)
} else {
self.displayResultLabel.text = UIPasteboard.general.string
}
print("Pasted")
})

最佳答案

您可以通过实例化 UIAlertController 来创建警报。您可以使用 onlyNumbers 函数的输出作为警报 Controller 的消息。

然后你需要添加 UIAlertAction(s) 到它最后调用 self.present 来显示来自 UIViewController 子类的警报 Controller .

if UIPasteboard.general.string?.onlyNumbers() == "" {
let alertController = UIAlertController(title: "Calc Pro", message: UIPasteboard.general.string?, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
self.present(alertController, animated: false, completion: nil)
}

关于ios - 来自扩展的 UIAlertController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46117734/

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