Ios 全局使用类
我在许多类中都有警报调用,我希望有一个警报类来处理所有的使用,也就是说,无论何时需要警报,都会调用该警报类。所有警报代码都在一个地方 现在,我在每一个出现的类中都重复了警报代码。我要一套警报代码Ios 全局使用类,ios,class,Ios,Class,我在许多类中都有警报调用,我希望有一个警报类来处理所有的使用,也就是说,无论何时需要警报,都会调用该警报类。所有警报代码都在一个地方 现在,我在每一个出现的类中都重复了警报代码。我要一套警报代码 如何操作?您需要为警报创建一个类 class MyCustomAlertView{ static let sharedUtils = MyCustomAlertView() class func showAlertOnVC(targetVC: UIViewController, title: Str
如何操作?您需要为警报创建一个类
class MyCustomAlertView{
static let sharedUtils = MyCustomAlertView()
class func showAlertOnVC(targetVC: UIViewController, title: String, message: String)
{
// write custom code here
let alert = UIAlertController(
title: title,
message: message,
preferredStyle: UIAlertControllerStyle.alert)
let okButton = UIAlertAction(
title:"OK",
style: UIAlertActionStyle.default,
handler:
{
(alert: UIAlertAction!) in
})
alert.addAction(okButton)
targetVC.present(alert, animated: true, completion: nil)
}}
在项目中需要调用警报的任何位置,请按以下方式调用:
MyCustomAlertView.showAlertOnVC(targetVC: self, title: "MyTitle", message: "test message") // whatever title and message you want
对于警报,您可以使用第三方库,例如:
- 目标C
- 斯威夫特
public class AlertUtils {
// Build classic Alert with OK Button
class func buildAlertInfo(withTitle title: String?, andMessage message: String?, withHandler handler: (UIAlertAction -> Void)?) -> UIAlertController {
let alertController = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: handler))
return alertController
}
// Build custom Alert with an OK Button and a redirection Button
class func buildAlertInfoWithFavButton(withTitle title: String?, andMessage message: String?, withHandler handler: [((UIAlertAction) -> Void)]?) -> UIAlertController {
let alertController = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: handler!.first))
alertController.addAction(UIAlertAction(title: "Favoris", style: UIAlertActionStyle.Default, handler: handler!.last))
return alertController
}
}
使用该类,您可以根据需要构建和自定义警报。您需要一个带有“确定”按钮和“设置”按钮的警报。
您可以创建尽可能多的自定义方法以生成特定警报
以下是您如何使用它:
let alert = AlertUtils.buildAlertInfo(withTitle: NSLocalizedString("alert_info_title", comment: ""),
andMessage: NSLocalizedString("alert_message", comment: ""), withHandler: nil)
self.presentViewController(alert, animated: false, completion: nil)
NB:请记住,在NSLocalizedString中重新组合所有字符串是最佳做法
处理程序的另一个示例:
let alert = AlertUtils.buildAlertInfoWithFavButton(
withTitle: "Alert Info"
andMessage: "Whatever you want",
withHandler: [okHandler, favoriteHandler]
)
self.presentViewController(alert, animated: false, completion: nil)
您还可以在处理程序中为警报按钮执行自定义操作
//Ok handler
func okHandler(action: UIAlertAction) {
//Do nothing
}
//Favorite handler to redirect to the Favorite View Controller
func favoriteHandler(action: UIAlertAction) {
self.navigationController?.pushViewController(FavoritesViewController(), animated: true)
}
为您的类创建一个类方法并编写公共alertview代码,这样您就不需要它的任何实例。 然后只需导入此泛型类,然后就可以在任何视图控制器中调用类方法,如:
[InternetAlert showAlert] 我写了一个很长的答案。我希望它能帮助你。顺便问一下,你是用Objective C还是Swift编码?你自己写了什么代码来尝试这样做?请不要只要求别人为你做这项工作。告诉我们你尝试过什么,什么不起作用,以证明你花了时间尝试帮助自己,这可以避免我们给出明显的答案,最重要的是,它可以帮助你得到更具体和相关的答案。也看到