全球使用的课程

时间:2017-02-23 13:55:20

标签: ios class

我在许多类中都有Alert调用,我希望有一个Alert类来处理所有用途,即需要Alert的地方,调用Alert类。所有警报代码都在一个地方。

现在我在其出现的每个班级都重复了警报代码。我想要一套警报代码。

我该怎么做?

3 个答案:

答案 0 :(得分:0)

您需要为警报创建一个类。

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

答案 1 :(得分:0)

对于警报,您可以使用第三方库,例如:

检查出来,非常有用。

此外,如果您想自己动手,可以创建自定义类。

例如:

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
    }
}

通过此课程,您可以按照自己的意愿构建和自定义警报。你想要一个带有" OK"的警报。按钮和"设置"纽扣。 您可以创建任意数量的自定义方法,以构建特定的警报。

以下是您使用它的方式:

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)
    }

答案 2 :(得分:0)

为您的类创建一个类方法并编写公共alertview代码,这样您就不需要它的任何实例。 然后只需导入这个泛型类,然后就可以在任何视图控制器中调用类方法,如:

[InternetAlert showAlert];

相关问题