将一些数据传回给RootViewController iOS 7 [英] Passing some data back to RootViewController iOS 7
问题描述
UINavigationController
中有3个ViewController,名为ViewControllerA,ViewControllerB,ViewControllerC。具体来说,ViewControllerA是 RootViewController
,并且ViewControllerB和ViewControllerC被推送到其上。所以,目前ViewControllerC在顶部,用户可以看到。 我想通过调用返回到ViewControllerA [self.navigationController popToRootViewControllerAnimated:YES] ;
方法,并从这里传递一些数据到ViewControllerA。我必须根据ViewControllerC传递的数据更新一些UI。
如果我不得不从ViewControllerB返回数据,那么我可以实现自定义协议/委托。但是在上述情况下可以做些什么好处?
提前感谢您的帮助。
您可以尝试NSNotificationCenter如下所示,
示例:
在您的ViewControllerA.m中
- (void)viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver: self selector:@selector(dataReceived :) name:@passDataobject:nil];
}
- (void)dataReceived:(NSNotification *)noti
{
NSLog(@dataReceived:%@,noti.object);
}
在您的ViewControllerC.m中
- (void)gotoHome
{
[[NSNotificationCenter defaultCenter] postNotificationName:@passData对象:[NSDictionary dictionaryWithObject:@Sample DataforKey :@dataDic]];
[self.navigationController popToRootViewControllerAnimated:YES];
}
谢谢!
Suppose , We have 3 ViewControllers called ViewControllerA , ViewControllerB , ViewControllerC in our UINavigationController
. To be specific , ViewControllerA is the RootViewController
and ViewControllerB and ViewControllerC are pushed on it. So, Currently ViewControllerC is on the Top and visible to user.
I want to return to ViewControllerA by calling [self.navigationController popToRootViewControllerAnimated:YES];
method and pass some data to ViewControllerA from here. I have to update some UI according to the data passed from ViewControllerC.
If I had to return data from ViewControllerB , then I could implement custom protocol/delegate. But what can be a good approach in the situation described above ?
Thanks in advance for your help.
You can try NSNotificationCenter as shown below,
Example:
In your ViewControllerA.m
-(void)viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dataReceived:) name:@"passData" object:nil];
}
-(void)dataReceived:(NSNotification *)noti
{
NSLog(@"dataReceived :%@", noti.object);
}
In your ViewControllerC.m
-(void)gotoHome
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"passData" object:[NSDictionary dictionaryWithObject:@"Sample Data" forKey:@"dataDic"]];
[self.navigationController popToRootViewControllerAnimated:YES];
}
Thanks!
这篇关于将一些数据传回给RootViewController iOS 7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!