这是我尝试自己做的第一个应用程序,我有一些疑问。我希望有4个选项卡,在第一个名为“HomeView”的选项卡中,我正在解析JSON数据(到目前为止已完成)。
但是我想要的是一些已解析为在其他选项卡中可见的数据(而不必再次解析它们)。
所以我的HomeView代码的一部分在这里:
#import "HomeView.h"
@interface HomeView ()
@end
@implementation HomeView
//other code
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
//data fetched
parsed_date=[res objectForKey:@"date"];
NSLog(@"Date:%@",parsed_date);
[UIAppDelegate.myArray addObject:parsed_date];
}
而且我可以看到“parsed_date”已正确打印。
因此,我希望此parsed_date在OtherView中可见。
这是我的代码,但我无法将其打印出来。
OtherView.m
#import "OtherView.h"
#import "HomeView.h"
#import "AppDelegate.h"
@interface OtherView ()
@end
@implementation OtherView
@synthesize tracks_date;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
//preview value of other class
tracks_date = [UIAppDelegate.myArray objectAtIndex:0];
NSLog(@"Dated previed in OtherView: %@", tracks_date);
}
并且(null)正在被打印出来。
添加了应用程序委托(delegate).h的代码
#import <UIKit/UIKit.h>
#define UIAppDelegate ((AppDelegate *)[UIApplication sharedApplication].delegate)
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (nonatomic, strong) NSArray *myArray;
@end
那你能建议我解决吗?
最佳答案
而是将属性添加到您的应用程序委托(delegate)中。
分配属性时,请执行以下操作:
MyAppDelegate *delegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
delegate.myProperty = @"My Value";
然后,您可以在不同的标签中以相同的方式检索此属性:
MyAppDelegate *delegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
NSString *valueInTab = delegate.myProperty;