2016-07-21 5 views
0

私のアプリケーションでプログレスバーを表示および非表示にするメソッドを定義したいと思います。しかし、私は各ビューコントローラでこれらのローカルメソッドを作成したくありません。どのようにビューをパラメータとして取るグローバルメソッドを定義し、それを使って何かをします。ここでviewcontrollerからアクセス可能な関数を作成するには?

は、私が試したものです:

import Foundation 
import MBProgressHUD 

public class ShowLoading{ 

    let view = UIView() 

    func show(){ 
     let loadingNotification = MBProgressHUD.showHUDAddedTo(view, animated: true) 
     loadingNotification.color = UIColor.blueColor() 
     loadingNotification.mode = MBProgressHUDMode.Indeterminate 
     loadingNotification.labelText = MyConstants.LOADING_TEXT 
    } 

    func hide(){ 
     MBProgressHUD.hideAllHUDsForView(view, animated: true) 
    } 

} 
+0

可能な複製(http://stackoverflow.com/questions/29262540/call-a-function-from-a-view) –

+0

メソッドを静的に定義し、次のようなパラメータを受け入れるようにします。static func test(view:UIView) – Dershowitz123

+0

AppDelegateクラスでメソッドを定義すると、簡単に提示されたView Controllerインスタンスを取得し、そのコントローラでhudを表示できます。ただし、アプリケーションのUIWindowインスタンスでhudを表示することもできます。 –

答えて

0

のUIViewController

と継承された1つのBaseViewControllerクラスは今すぐどこあなたにViewControllerをこのBaseViewControllerを継承BaseViewController

func show() { 
let loadingNotification = MBProgressHUD.showHUDAddedTo(view, animated: true) 
    loadingNotification.color = UIColor.blueColor() 
    loadingNotification.mode = MBProgressHUDMode.Indeterminate 
    loadingNotification.labelText = MyConstants.LOADING_TEXT 
} 

func hide(){ 
    MBProgressHUD.hideAllHUDsForView(view, animated: true) 
} 

であなたの方法を作成してください進行状況バーを表示したい場合は、self.show()self.hide()

0

let view = UIView()を削除するだけで、その必要はありません。 などの方法にパラメータを追加します。

func show(view: UIView) { 

    let loadingNotification = MBProgressHUD.showHUDAddedTo(view, animated: true) 
    loadingNotification.color = UIColor.blueColor() 
    loadingNotification.mode = MBProgressHUDMode.Indeterminate 
    loadingNotification.labelText = MyConstants.LOADING_TEXT 
} 
[ビューから関数を呼び出す]の
関連する問題