在iOS中的选项卡之间传递数据

gho*_*der 5 tabs cocoa-touch objective-c ios

这是我自己尝试做的第一个应用程序,我有一些问题.我想要有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];
        }
Run Code Online (Sandbox Code Playgroud)

我可以看到"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);
}
Run Code Online (Sandbox Code Playgroud)

和(null)正在打印出来.

添加了app 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
Run Code Online (Sandbox Code Playgroud)

所以你能建议我解决吗?

sco*_*ord 11

请将属性添加到Application Delegate.

分配属性时,请执行以下操作:

MyAppDelegate *delegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];

delegate.myProperty = @"My Value";
Run Code Online (Sandbox Code Playgroud)

然后,在不同的选项卡中,您可以以相同的方式检索此属性:

MyAppDelegate *delegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
NSString *valueInTab = delegate.myProperty; 
Run Code Online (Sandbox Code Playgroud)