Ios 如何将警报消息添加到全局类
我的许多Ios 如何将警报消息添加到全局类,ios,swift,Ios,Swift,我的许多视图控制器中都重复了以下代码。我想把它重构成一个全局类/视图控制器。但是,我仍然希望它与父视图一起显示为当前视图控制器,这样当我单击“确定”时,它将返回到发生错误的当前视图控制器。该全局类的名称应该是什么?它应该是UIViewController的子类吗 //-------------------------------------- // MARK: - Alert Error Message //--------------------------------
视图控制器中都重复了以下代码。我想把它重构成一个全局类/视图控制器。但是,我仍然希望它与父视图一起显示为当前视图控制器,这样当我单击“确定”时,它将返回到发生错误的当前视图控制器。该全局类的名称应该是什么?它应该是UIViewController的子类吗
//--------------------------------------
// MARK: - Alert Error Message
//--------------------------------------
func displayAlertMessage(title:String,message:String)
{
let alertMessage = UIAlertController(title: title, message: message, preferredStyle:UIAlertControllerStyle.alert);
let okAction = UIAlertAction(title:"OK", style: .default, handler:nil);
alertMessage.addAction(okAction);
self.present(alertMessage, animated: true, completion: nil);
}
我喜欢这个的扩展
如果扩展uiviewcontroller,您应该能够将此函数放入其中,然后可以从任何视图控制器调用它。我喜欢此扩展
如果扩展uiviewcontroller,您应该能够将此函数放入其中,然后可以从任何视图控制器调用它。您可以创建一个uiviewcontroller
扩展并在那里实现此方法。这将允许您从任何UIViewController
子类调用它
extension UIViewController {
func displayAlertMessage(title: String, message: String)
{
let alertMessage = UIAlertController(title: title, message: message, preferredStyle:UIAlertControllerStyle.alert);
let okAction = UIAlertAction(title:"OK", style: .default, handler:nil);
alertMessage.addAction(okAction);
self.present(alertMessage, animated: true, completion: nil);
}
}
您还可以将处理程序
和完成
参数添加到displayAlertMessage
函数中,这将允许您自定义操作处理程序和警报完成时发生的情况,从调用点。您可以创建一个UIViewController
扩展并在那里实现此方法。这将允许您从任何UIViewController
子类调用它
extension UIViewController {
func displayAlertMessage(title: String, message: String)
{
let alertMessage = UIAlertController(title: title, message: message, preferredStyle:UIAlertControllerStyle.alert);
let okAction = UIAlertAction(title:"OK", style: .default, handler:nil);
alertMessage.addAction(okAction);
self.present(alertMessage, animated: true, completion: nil);
}
}
您还可以将处理程序
和完成
参数添加到displayAlertMessage
函数中,这将允许您从调用点自定义操作处理程序和警报完成时发生的情况。Swift 4.0
可以对多个操作使用操作闭包
extension UIViewController {
func popupAlert(title: String?, message: String?, actionTitles:[String?], actions:[((UIAlertAction) -> Void)?]) {
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
for (index, title) in actionTitles.enumerated() {
let action = UIAlertAction(title: title, style: .default, handler: actions[index])
alert.addAction(action)
}
self.present(alert, animated: true, completion: nil)
}
}
您可以在UIViewController中这样使用
popupAlert(title: kTitle, message: "This is test alert!!!!" , actionTitles: ["Ok","Cancel"], actions: [ { action1 in
//perform action for OK button
}, { action2 in
//perform action for cancel button
}])
Swift 4.0
可以对多个操作使用操作闭包
extension UIViewController {
func popupAlert(title: String?, message: String?, actionTitles:[String?], actions:[((UIAlertAction) -> Void)?]) {
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
for (index, title) in actionTitles.enumerated() {
let action = UIAlertAction(title: title, style: .default, handler: actions[index])
alert.addAction(action)
}
self.present(alert, animated: true, completion: nil)
}
}
您可以在UIViewController中这样使用
popupAlert(title: kTitle, message: "This is test alert!!!!" , actionTitles: ["Ok","Cancel"], actions: [ { action1 in
//perform action for OK button
}, { action2 in
//perform action for cancel button
}])
只需创建一个新的NSObject类并执行以下操作即可
class WrapperClass: NSObject
{
class func BasicAlert(_ title : String, message : String, view:UIViewController)
{
let alert = UIAlertController(title:title, message: message, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
view.present(alert, animated: true, completion: nil)
}
}
用法
WrapperClass.BasicAlert("Error", message: "Bluetooth Headset is Not Connected, Please Retry", view: self)
只需创建一个新的NSObject类并执行以下操作即可
class WrapperClass: NSObject
{
class func BasicAlert(_ title : String, message : String, view:UIViewController)
{
let alert = UIAlertController(title:title, message: message, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
view.present(alert, animated: true, completion: nil)
}
}
用法
WrapperClass.BasicAlert("Error", message: "Bluetooth Headset is Not Connected, Please Retry", view: self)
您可以使用来创建简单的警报。警报始终从当前视图控制器显示,并在警报关闭时返回到此控制器。此静态函数显示一条带有OK按钮的简单消息。当然,您还可以添加文本字段、按钮和其他元素。这些警报特别好,因为您可以在代码中的任何时候调用它们,甚至可以从后台队列调用它们
class Alerts {
static func showAlert(title: String, message: String) {
DispatchQueue.main.async {
let alert = SCLAlertView()
alert.showCustom(title, subTitle: message, color: UIColor.blue, icon: UIImage(named: "logo")!, closeButtonTitle: "OK")
}
}
}
您可以使用来创建简单的警报。警报始终从当前视图控制器显示,并在警报关闭时返回到此控制器。此静态函数显示一条带有OK按钮的简单消息。当然,您还可以添加文本字段、按钮和其他元素。这些警报特别好,因为您可以在代码中的任何时候调用它们,甚至可以从后台队列调用它们
class Alerts {
static func showAlert(title: String, message: String) {
DispatchQueue.main.async {
let alert = SCLAlertView()
alert.showCustom(title, subTitle: message, color: UIColor.blue, icon: UIImage(named: "logo")!, closeButtonTitle: "OK")
}
}
}
为什么不将其作为扩展写入UIViewController
?为什么不将其作为扩展写入UIViewController
?