簡體   English   中英

從另一個視圖控制器通知視圖控制器

[英]Notify view controllers from another view controller

我在ViewController中有一個細分控件和一個容器。 在容器中是一個PageViewController。 如果單擊細分控件中的一項,則表“用戶”表的“活動”值將更改。(sqlite)表“用戶”:

username text, active bool
user1, false
user2,true

因此,如果您在細分控件中單擊user1,則該表將更改為

username text, active bool
    user1, true
    user2,false

有沒有一種方法可以使PageViewController中的所有ViewController識別數據庫中的更改,並用新用戶重新加載其數據。 如果這不可能,那么最好的方法是什么?

親切的問候

如果我理解正確,則希望在其他視圖控制器中更改分段控件的值時通知一些視圖控制器。

在這種情況下,您可以使用NSNotificationCenter

NsNotificationCenter將通知發送給其觀察者。 當觀察者收到通知時,您可以使其執行某些操作。

例:

NSNotificationCenter.defaultCenter().postNotificationName("NotificationIdentifier", object: nil)

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(aMethod(_:)), name:"NotificationIdentifier", object: nil)

func aMethod(notification: NSNotification){
  // Do stuff here
}

暫無
暫無

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

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