[英]how to refresh the viewdidload for parent viewcontroller
我有2個帶有segue“頁面卷曲”的viewcontrollers
viewcontrollerA => pagecurl => viewcontrollerB,現在我想更新viewcontrollerA,因為用戶在viewcontrollerB上做了一些更改。
我嘗試過:
UIStoryboard* sb = [UIStoryboard storyboardWithName:@"mystoryboard"
bundle:nil];
UIViewController* vc = [sb instantiateViewControllerWithIdentifier:@"ExampleViewController"];
[vc ViewDidLoad]; // or ViewWillAppear or ViewDidApear
它僅適用於我放入這些功能的NSLog。 但是它們都不與檢出Coredata並更新接口的功能一起使用。 請幫忙
試試這個代碼:
您添加父類
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(actionremovecalender:)
name:@"subMitReport"
object:nil];
-(void)actionremovecalender:(NSNotification *)notification
{
[self ViewDidLoad]
}
召集孩子班
[[NSNotificationCenter defaultCenter]postNotificationName:@"subMitReport" object:nil]
您可以發送父級將收到的NSNotification,也可以使用父級視圖實現的方法設置委托。
在這兩種情況下,只需重新加載視圖即可。
在一對一關系中,您應該首選委托模式。 只需將viewcontrollerA的弱引用添加到viewcontrollerB。 您可以使用引用僅調用viewcontrollerA的方法(在本例中為viewDidLoad方法),以便刷新視圖。 但是我更願意聲明一個委托協議,以防止兩個視圖控制器的緊密耦合。
由於Viewcontroller中有任何對象更改,因此將調用ViewWillApear或ViewDidApear,但是如果要從另一個viewcontrollerB更改viewcontrollerA,則需要NSNotificationCenter從viewcontrollerA調用該函數
您可以隨時使用NSNotificationCenter更新您的父級ViewController
在您的父級viewcontroller上放置:
//since child view controller calls turnItOff, notification center calls function "turnButtonCountinuesOff"
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(turnButtonCountinuesOff) name:@"turnItOff" object:nil];
turnButtonCountinuesOff是父視圖控制器上的函數
在您的孩子的viewcontroller上放置:
//connect to parent UI view controller calls notification turnItOff.
[[NSNotificationCenter defaultCenter] postNotificationName:@"turnItOff" object:nil];
希望能幫助到你。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.