2017-04-12 73 views
0

我试图声明一个函数来在我的应用程序中显示警报。为了避免重复工作,我试图为我的所有应用程序使用相同的功能。我试图通过创建一个具有函数showNotification的类来实现这一点。但是当我创建该类的对象并调用方法时,没有任何反应。我怎样才能做到这一点?在所有视图控制器中创建警报功能 - swift

class SharedPropertiesAndMetods : UIViewController { 

    func showNotification(title: String, message: String) 
    { 
     let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert) 
     let defaultAction = UIAlertAction(title: "تائید", style: .default, handler: nil) 
     alertController.addAction(defaultAction) 
     present(alertController, animated: true, completion: nil) 
    } 

} 
+1

添加此方法的UIViewController – Anuraj

+0

@HosAp的延伸有4个答案。他们都没有帮助吗? –

+0

@GaneshKumar是的,作为扩展添加是一个好主意 –

回答

2

我会怎么做,是创建做的工作,不是从它继承一个“通用”视图控制器:

1.如果你想显示警报,每次查看都出现:

class GenericViewController: UIViewController { 

    // MARK: - View lifecycle - 

    override func viewDidAppear(_ animated: Bool) { 
     super.viewDidAppear(animated) 
     if let notification = self.shouldDisplayAlertNotification() { 
      self.showNotification(notification) 
     } 
    } 

    // MARK: - Internal methods - 

    func shouldDisplayAlertNotification() -> AlertNotification? { 
     return nil 
    } 

    // MARK: - Private methods - 

    private func showNotification(_ alertNotification: AlertNotification) { 
    } 

} 

class MyController: GenericViewController { 

    override func shouldDisplayAlertNotification() -> AlertNotification? { 
     return AlertNotification(title: "Title", message: "Message") 
    } 

} 

哪里AlertNotification是您的自定义模型类:

class AlertNotification { 
    var title: String 
    var message: String 

    init(title: String, message: String) { 
     self.title = title 
     self.message = message 
    } 
} 

这样,只有覆盖shouldDisplayAlertNotification的VC才会显示警报。

2.如果你想显示在“需求”警告:

至于建议,延长的UIViewController

extension UIViewController { 
    func showNotification(title: String, message: String) { 
    } 
} 
+0

这样,当我想在VC中使用viewDidAppear时,它将覆盖GenericViewContoller的viewDidAppear。不是吗? –

+0

是的,但你可以打电话超级。viewDidAppear作为第一条指令,所以会显示提示 –

1

其实你可以在类之外的任何地方声明一个简单的方法。

func showAlertWithCompletion(message:String,okTitle:String,cancelTitle:String?,completionBlock:@escaping (_ okPressed:Bool)->()){ 
    let alertController = UIAlertController(title: AppName, message: message, preferredStyle: .alert) 
    let okAction = UIAlertAction(title: okTitle, style: .default) { (ok) in 
     completionBlock(true) 
    } 
    alertController.addAction(okAction) 
    if let cancelTitle = cancelTitle{ 
     let cancelOption = UIAlertAction(title: cancelTitle, style: .cancel, handler: { (axn) in 
      completionBlock(false) 

     }) 
     alertController.addAction(cancelOption) 
    } 

    if let topController = UIWindow.topViewController(){ 
     topController.present(alertController, animated: true, completion: nil) 
    } 

} 

这样,无论你怎么称呼它,你会得到确定按钮按下回调完成手柄甚至使扩展由@Ganesh库马尔

2

使用延长这样

extension UIViewController { 
    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) 
    } 
} 

描述打电话这样的功能

self.showAlert(title: "hi", message: "test") 
1

为什么不只是一个扩展

extension UIViewController { 

    func showNotification(title: String, message: String) 
    { 
     let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert) 
     let defaultAction = UIAlertAction(title: "تائید", style: .default, handler: nil) 
     alertController.addAction(defaultAction) 
     present(alertController, animated: true, completion: nil) 
    } 
} 
0

你也可以在你的应用程序创建一个文件的util中,你可以添加任何可重复使用的方法或功能,在您的应用程序就像在任何地方使用它,

进口基金会 进口的UIKit

// MARK: - ALERT

FUNC showMessage(标题:字符串,消息:字符串!VC:UIViewController中){

let alert : UIAlertController = UIAlertController(title: "", message: message, preferredStyle: UIAlertControllerStyle.alert) 
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) { 
    UIAlertAction in 
} 
alert.addAction(okAction) 
VC.present(alert, animated: true, completion: nil) 

}

相关问题