[英]Pass data to from one viewcontroller to another viewcontroller on the same view
[英]Pass data from one viewcontroller to existing viewcontroller programmatically
最近我陷入了一个问题,所以我无法继续下一个编码部分。 有两个视图控制器(比如first.m和second.m),我想在不使用故事板的情况下将参数数据从一个视图控制器传递到另一个视图控制器。 但对我来说棘手的部分是第二个视图控制器已经在AppDelegate.m文件中启动。
如果我在第一个视图控制器中启动了第二个视图控制器并带有一些导入头文件,我可以传递数据,例如..
@import "Second.h"
Second *secondView = [[Second alloc] initWithNibName:@"SecondNib" bundle:nil];
secondView.someStringValue = @"passThisString";
但是,这种编码会产生一个新的第二个视图控制器,这不是我想要的。 我发现有类似的东西
myAppDelegate *appDelegate = (myAppDelegate *)[[UIApplication sharedApplication] delegate];
连接到AppDelegate.m文件。 但经过几天的挣扎,我的大脑正在停电。 请给我任何建议。有没有办法将数据传递给现有的视图控制器而不使用故事板? 或通过AppDelegate到第二个视图控制器的任何方式。 任何帮助将不胜感激
@ jazzed28你可以在不启动你的Second
情况下做到这一点。 您只需要访问已经启动的Second
。 所以在myAppDelegate.h
声明Second
作为它的property
,就像这样 -
@property (nonatomic, strong) Second *svc;
那么无论什么时候你需要秒,你都可以这样访问 -
myAppDelegate *appDelegate = (myAppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.svc
myteDelegate中用于设置数据和从Appdelegate获取数据的两个类方法,您可以使用APpdelegate类名访问这些方法。
Setter method
+(void)setGlobalString:(NSString *)stringgValue{
[[NSUserDefaults standardUserDefaults] setObject:stringgValue forKey:@"globalValue"];
// you can save string value here by using NSUserDefaults or any thing else
}
Getter method
+(NSString *)getGlobalString{
return [[NSUserDefaults standardUserDefaults] forKey:@"globalValue"];
}
你可以调用像这样的方法
[myAppDelegate setGlobalString:yourString];
NSString *str = [myAppDelegate getGlobalString];
之前已经多次询问过这个问题,包括StackOverflow。 如果你花了一些时间寻找答案,你可能已经找到了这个答案 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.