将一些数据传递回RootViewController iOS 7
假设在我们的将一些数据传递回RootViewController iOS 7,ios,objective-c,uiviewcontroller,uinavigationcontroller,delegates,Ios,Objective C,Uiviewcontroller,Uinavigationcontroller,Delegates,假设在我们的UINavigationController中有三个名为ViewControllerA、ViewControllerB和ViewControllerC的viewcontroller。具体来说,ViewControllerA是根ViewController,ViewControllerB和ViewControllerC被推到其上。因此,目前ViewControllerC位于顶部,用户可以看到 我想通过调用[self.navigationController poptrootviewco
UINavigationController
中有三个名为ViewControllerA、ViewControllerB和ViewControllerC的viewcontroller。具体来说,ViewControllerA是根ViewController,ViewControllerB和ViewControllerC被推到其上。因此,目前ViewControllerC位于顶部,用户可以看到
我想通过调用[self.navigationController poptrootviewcontrolleranimated:YES]返回ViewControllerA编码>方法并从这里将一些数据传递给ViewControllerA。我必须根据ViewControllerC传递的数据更新一些UI
如果必须从ViewControllerB返回数据,则可以实现自定义协议/委托。但在上述情况下,什么是好的方法
提前感谢您的帮助。最好的方法是在不同的类中分离共享信息(我建议,这将是一些模型类)。只需在两个视图控制器中存储相同的模型类实例,并在模型类更改时更新它们。使用通知、KVC或ask model state every viewdide:method。最好的方法是在不同的类中分离共享信息(我建议,它将是某个模型类)。只需在两个视图控制器中存储相同的模型类实例,并在模型类更改时更新它们。使用通知、KVC或ask model state every VIEWDIDISPENCE:方法。您可以尝试如下所示的NSNotificationCenter
例如:
在你的视野中
-(void)viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dataReceived:) name:@"passData" object:nil];
}
-(void)dataReceived:(NSNotification *)noti
{
NSLog(@"dataReceived :%@", noti.object);
}
在您的ViewControllerC.m中
-(void)gotoHome
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"passData" object:[NSDictionary dictionaryWithObject:@"Sample Data" forKey:@"dataDic"]];
[self.navigationController popToRootViewControllerAnimated:YES];
}
您可以尝试NSNotificationCenter,如下所示
例如:
在你的视野中
-(void)viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dataReceived:) name:@"passData" object:nil];
}
-(void)dataReceived:(NSNotification *)noti
{
NSLog(@"dataReceived :%@", noti.object);
}
在您的ViewControllerC.m中
-(void)gotoHome
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"passData" object:[NSDictionary dictionaryWithObject:@"Sample Data" forKey:@"dataDic"]];
[self.navigationController popToRootViewControllerAnimated:YES];
}
在这里,您可以使用委托方法
这是调用委托方法的根ViewController
#import "ThirdViewController.h"
#import "SecondViewController.h"
@interface ViewController ()<ThirdViewControllerDelegate>
@end
@implementation ViewController
#pragma mark -
#pragma mark ThirdViewController Delegate Method
将最后一个Vc委托传递给下一个ViewController
-(void)didSelectValue:(NSString *)value{
NSLog(@"%@",value);
}
-(void)gotoSeconddVc{
SecondViewController *vc2=[[SecondViewController alloc]init];
vc2.lastDelegate=self;
[self.navigationController pushViewController:vc2 animated:YES];
}
#import "ThirdViewController.h"
@interface SecondViewController : UIViewController
@property(nonatomic,retain) id <ThirdViewControllerDelegate> lastDelegate;
@end
-(void)gotoThirdVc{
ThirdViewController *vc3=[[ThirdViewController alloc]init];
vc3.delegate=self.lastDelegate;
[self.navigationController pushViewController:vc3 animated:YES];
}
在这里,您可以使用委托方法
这是调用委托方法的根ViewController
#import "ThirdViewController.h"
#import "SecondViewController.h"
@interface ViewController ()<ThirdViewControllerDelegate>
@end
@implementation ViewController
#pragma mark -
#pragma mark ThirdViewController Delegate Method
将最后一个Vc委托传递给下一个ViewController
-(void)didSelectValue:(NSString *)value{
NSLog(@"%@",value);
}
-(void)gotoSeconddVc{
SecondViewController *vc2=[[SecondViewController alloc]init];
vc2.lastDelegate=self;
[self.navigationController pushViewController:vc2 animated:YES];
}
#import "ThirdViewController.h"
@interface SecondViewController : UIViewController
@property(nonatomic,retain) id <ThirdViewControllerDelegate> lastDelegate;
@end
-(void)gotoThirdVc{
ThirdViewController *vc3=[[ThirdViewController alloc]init];
vc3.delegate=self.lastDelegate;
[self.navigationController pushViewController:vc3 animated:YES];
}
使用singleton类将信息从lastView控制器共享到first。您还将使用NSNotificationCenter将数据发送到任何正在运行的视图控制器。在这种情况下,使用localNotification共享信息使用委托将数据从一个视图控制器移动到另一个控制器使用singleton类共享lastView中的信息控制器。您还将使用NSNotificationCenter将数据发送到任何正在运行的视图控制器。在这种情况下,使用localNotification共享信息使用代理将数据从一个视图控制器移动到另一个控制器