在 swift 的范围错误中找不到 'UIAlertController'?
Cannot find 'UIAlertController' in scope error in swift?
如果我在单独的 swift 文件中编写警报功能,或者如果我使用扩展,它不会出现在另一个文件中,为什么我会收到 Cannot find 'UIAlertController' in scope?
错误 viewcontroller 为什么?
代码:
func showAlert(title: String, message: String) {
let alertController = UIAlertController(title: title, message:
message, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: {action in
}))
self.present(alertController, animated: true, completion: nil)
}
错误:
Cannot find 'UIAlertController' in scope,
Cannot find 'UIAlertAction' in scope
Cannot find 'self' in scope
可能您在 viewController:
中忘记了这个
import UIKit
所以您的文件不知道 UIAlertController
或 UIAlertAction
是什么
如果你想创建一个警报视图控制器来跨多个 UIViewController 使用,你可以这样做:
import UIKit
class CustomAlertController: NSObject {
let message:String?
let title:String?
init(title:String, message:String) {
self.message = message
self.title = title
}
func showAlert()->UIAlertController {
let alertController = UIAlertController(title: self.title, message: self.message, preferredStyle: .alert)
// you can further customize your buttons, buttons' title etc
alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: {action in
}))
return alertController
}
}
然后是你的视图控制器
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let alert = CustomAlertController(title: "Hello", message: "My message to the world")
DispatchQueue.main.async {
self.present(alert.showAlert(), animated: true, completion: nil)
}
}
}
如果我在单独的 swift 文件中编写警报功能,或者如果我使用扩展,它不会出现在另一个文件中,为什么我会收到 Cannot find 'UIAlertController' in scope?
错误 viewcontroller 为什么?
代码:
func showAlert(title: String, message: String) {
let alertController = UIAlertController(title: title, message:
message, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: {action in
}))
self.present(alertController, animated: true, completion: nil)
}
错误:
Cannot find 'UIAlertController' in scope,
Cannot find 'UIAlertAction' in scope
Cannot find 'self' in scope
可能您在 viewController:
中忘记了这个import UIKit
所以您的文件不知道 UIAlertController
或 UIAlertAction
是什么
如果你想创建一个警报视图控制器来跨多个 UIViewController 使用,你可以这样做:
import UIKit
class CustomAlertController: NSObject {
let message:String?
let title:String?
init(title:String, message:String) {
self.message = message
self.title = title
}
func showAlert()->UIAlertController {
let alertController = UIAlertController(title: self.title, message: self.message, preferredStyle: .alert)
// you can further customize your buttons, buttons' title etc
alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: {action in
}))
return alertController
}
}
然后是你的视图控制器
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let alert = CustomAlertController(title: "Hello", message: "My message to the world")
DispatchQueue.main.async {
self.present(alert.showAlert(), animated: true, completion: nil)
}
}
}