這是我嘗試自己做的第一個應用程序,我有一些問題。我想要4個選項卡,並且在名爲「HomeView」的第一個選項卡中,我正在解析JSON數據(至此已完成)。在iOS中的標籤之間傳遞數據
但我想要的是一些正在被解析的數據在其他選項卡中可見(而不必再次解析它們)。
所以我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);
}
和(空)被打印出來。
添加的應用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
所以你能建議我一個sollution?
myProperty是我的@property的名稱還是不是?我應該將這段代碼放在AppDelegate.m文件中嗎? – ghostrider
myProperty是@property的名稱,您可以將其命名爲任何您想要的名稱。您應該放置我在視圖控制器中編寫的代碼片段。 – scord
所以@屬性(非原子,保留)NSString * myProperty會去你的應用程序委託.h文件 – scord