簡體   English   中英

如何從 viewController 到 appdelegate 進行通信?

[英]How do a comunication from viewController to appdelegate?

我想與我的 appdelegate 通信,換句話說,我想從多個視圖向 AppDelegate 發送數據,這可能嗎?

我找到了這個代碼

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

這個問題的: Bi-Directional communication between ViewController and AppDelegate with swift 2 for iOS

NSNotification 可以進行通信嗎?

是的, Notification可以做你想做的事。 從視圖控制器中,發布通知:

class YourViewController: UIViewController {

    ...

    @IBAction func buttonPressed(_ sender: Any) {
        NotificationCenter.default.post(name: "ViewControllerButtonPressed", object: nil)
    }
}

然后,在你的AppDelegate ,添加一個Notification觀察者來觀察這個通知名稱的帖子:

class AppDelegate: UIResponder, UIApplicationDelegate {

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        ...

        //Add the observer
        //This observer will call the "doSomething" function every time NotificationCenter.default.post(name: "ViewControllerButtonPressed", object: nil) is called
        NotificationCenter.default.addObserver(self, selector: #selector(doSomething), name: "ViewControllerButtonPressed", object: nil)
    }

    @objc func doSomething() {
        print("A button was pressed in one of your view controllers!")
    }

}

另一個建議:為了讓事情更簡單,您可以擴展Notification.Name為名稱創建靜態String值:

public extension Notification.Name {
    static let ButtonPressed = Notification.Name("ViewControllerButtonPressed")
}

這可以取代

NotificationCenter.default.post(name: "ViewControllerButtonPressed", object: nil)

NotificationCenter.default.post(name: NSNotification.Name.ButtonPressed, object: nil)

NotificationCenter.default.addObserver(self, selector: #selector(doSomething), name: "ViewControllerButtonPressed", object: nil)

NotificationCenter.default.addObserver(self, selector: #selector(doSomething), name: NSNotification.Name.ButtonPressed, object: nil)

實現上述方法是可選的,但可以防止在使用Notification時出現拼寫錯誤。

我不建議您使用通知中心。 他們有一些限制。

  1. 在每個 ViewController 中,您必須使用 post 函數,而在 App Delegate 中,您必須添加觀察者。 那么只有你可以使用NotificationCenter的post方法。 就像上面大衛向你展示的那樣。

  2. 如果您必須從不同的視圖控制器調用 App Delegate 的不同功能,那么您必須再次在 App Delegate 中添加不同的觀察者。 之后就可以調用App delegate的不同功能了。

但是使用您在問題中定義的上述方法。 您不需要添加觀察者。 借助此實例,您可以調用 App 委托的任何方法。 我會給你看一個例子:-

 let appDelegate = UIApplication.shared.delegate as! AppDelegate

 @UIApplicationMain
 @objc class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?
var isUpdate: Bool = false

func setValuesOfAppDelegate() {
    print("123")
}

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

    IQKeyboardManager.shared.enable = true

    if UserDefaults.standard.bool(forKey: UserDefaultKey.isAutoLogin) == false {
        startViewController()
    } else {
        goToRootViewAfterLogin()
    }

    AppUserDefaults.set(KEnglish, forKey: KSetLanguage)
    self.registerForPushNotifications(application)

    return true
  }
}

示例如何使用 App 委托實例中的 App 委托變量和函數。 你可以看看代碼。

  1. 在第一個示例中,我從 ClassA 設置 App Delegate 的變量。

  2. 在第二個示例中,我可以從 ClassB 調用 App Delegate 的功能。

  3. 在第三個示例中,可以設置 App Delegate Class 變量的值並同時訪問該函數。

     class ClassA: UIViewController { //MARK:- View Life Cycle override func viewDidLoad() { super.viewDidLoad() appDelegate.isUpdate = true } } class ClassB: UIViewController { //MARK:- View Life Cycle override func viewDidLoad() { super.viewDidLoad() appDelegate.setValuesOfAppDelegate() } } class ClassC: UIViewController { //MARK:- View Life Cycle override func viewDidLoad() { super.viewDidLoad() appDelegate.isUpdate = true appDelegate.setValuesOfAppDelegate() } }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM