2012-04-17 34 views
0

我的XML解析器有一些問題,用obj-c編寫。 解析器本身很好,但我不能訪問DetailView中的結果數組。如何在另一個視圖中訪問NSArray

我需要由我的主和DetailViewController中的解析器類創建的數組值。

在MasterViewController我這樣做:

MasterViewController.h

#import <UIKit/UIKit.h> 
#import "AppDelegate.h" 
#import "TouchXML.h" 
#import "Parser.h" 

@class Parser; 

@interface MasterViewController : UITableViewController{ 
    Parser *theParser; 
    } 

MasterViewController.m

- (void)viewDidLoad 
{ 
    theParser = [[Parser alloc] init]; 
    [theParser startParser:xmlPath]; //Now the parser runs and parses all needed values into arrays 
} 

然後我推點擊的DetailView,我想有訪問值太。

UniViewController.h

#import <UIKit/UIKit.h> 
#import "AppDelegate.h" 
#import "Parser.h" 

@interface UniViewController : UITableViewController{ 

    Parser *theParser; 
} 

UniViewController.m

- (void)viewDidLoad 
{ 
    NSLog(@"Name: %@",[[theParser.listArray objectAtIndex: 0]valueForKey:@"name"]); 
} 

在這裏,我想從解析器訪問數組,但我總是得到的值(NULL)? 在調試器中,我看到PERSER有0x0000,不能正確... 在執行了Parser = [[Parser alloc] init]之後;在這裏,它有一個十六進制值,但我仍然得到(null)。

如何從DetailView訪問數組值?

如果有人能在這裏向我解釋問題,那真的很不錯。

謝謝。

回答

0

您的詳細視圖必須有一個指向您的馬西德威(通過解析器)創建數組,試試這個:

(void)viewDidLoad 
{ 
    aParser = [[Parser alloc] init]; 
    [aParser startParser:xmlPath]; 
    myUniView.theParser = aParser; //myUniView is just the detail view you're pushing 

,然後在此之後,你可以把你的詳細信息視圖。

+0

作品完美,謝謝! – Nico 2012-04-18 00:54:45

+0

我怎樣才能做到這一點與普通班,而不是視圖?我試圖訪問我的數組在MapView的Annotation類。在MasterViewController中...在我推送DetailView之前,我這樣做:Annotation * Anno = [[Annotation alloc] init]; Anno.theParser = theParser; 但我無法訪問Annotation.m類中的數組? – Nico 2012-04-18 18:31:26

+0

是當您將其分配給註釋時已經分配的解析器嗎?你是否在整個應用程序中只使用一個分析器?如果是這樣,你可以考慮讓你的解析器類是一個單例,以便你可以從任何類中獲得它 – 2012-04-18 18:36:42

相關問題