2017-04-12 72 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) 

}

相關問題