2012-08-27 32 views
5

這是我嘗試自己做的第一個應用程序,我有一些問題。我想要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?

回答

11

將屬性添加到您的應用程序委託中。

在分配財產做這樣的事情:

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

delegate.myProperty = @"My Value"; 

然後,在你不同的標籤,你可以檢索以同樣的方式將此屬性:

MyAppDelegate *delegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate]; 
NSString *valueInTab = delegate.myProperty; 
+0

myProperty是我的@property的名稱還是不是?我應該將這段代碼放在AppDelegate.m文件中嗎? – ghostrider

+0

myProperty是@property的名稱,您可以將其命名爲任何您想要的名稱。您應該放置我在視圖控制器中編寫的代碼片段。 – scord

+0

所以@屬性(非原子,保留)NSString * myProperty會去你的應用程序委託.h文件 – scord

1

雖然這可能不是最好的這樣做的方法很簡單而且有效。

將您的數據保存到您的應用程序委託中,並從那裏檢索它。您可以爲您的應用程序委託共享應用程序創建快捷方式。然後只需訪問那裏的值。

AppDelegate.h 

#define UIAppDelegate ((AppDelegate *)[UIApplication sharedApplication].delegate) 

@property (nonatomic, strong) NSArray *myArray; 

TabView1.m 

#import "AppDelegate.h" 

SomeObject *myObject = [UIAppDelegate.myArray objectAtIndex:0]; 

就像我說的,它可能不是組織你的數據,你的應用程序的最佳方法,這種方法對於少量需要在應用層面上共享數據的工作。希望這可以幫助。

+0

哈!也許這是比我想象的更流行的方法。 ;) –

+0

我dit你說什麼,但我仍然得到空。你能看到我的編輯? – ghostrider

2

呃,當你在最後的代碼段有創建HomeView,你要創建一個新的對象 - 一個新的實例的類的。它將不包含來自connectionDidFinishLoading的數據,除非該方法在該類的實例的中執行。

您基本上需要使用某種持久性機制來做你想做的事情,或者是沿着「單例」行的AppDelegate或靜態存儲。

+1

順便說一句,這是面向對象的101東西,在開始Objective-C編程之前,你應該已經學會了它。 –

+4

雖然你的回答是正確的,但增加的評論並不是真的有必要。 –

1

發生這種情況是因爲您自己創建了一個HomeView的實例。它與任何東西根本沒有關係。
你的第一個例子工作,因爲它是從你的筆尖創建和初始化。

我認爲最好的方法是使用IBOutlet然後在InterfaceBuilder中連接兩個'視圖'。

@interface OtherView() 
    IBOutlet HomeView *homeView; 
@end 

@implementation OtherView 
@synthesize tracks_date; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    NSLog(@"Dated previed in OtherView: %@", homeView.parsed_date); 
} 

- (void)dealloc: 
{ 
    [homeView release]; 
} 

InterfaceBuilder下,您可以管理你的對象並連接(通過IBOutlets和IBAction爲......)在一起。

我認爲this video是一個很好的演示這個概念如何工作。

+0

我仍然得到(null)。如何在界面構建器中連接這些視圖?在你說的鏈接中,標題和按鈕說的不是整個視圖。 – ghostrider

+0

你從哪裏獲得對其他視圖的可尋址性? –

+1

這是通過選項卡控制器完成的。第一個選項卡是HomeView和OtherView是我的第二個選項卡,因此它們不直接連接(如果這是你的意思) – ghostrider

相關問題