2011-08-16 38 views
0

後,我有作爲XML解析器工作的一類,想創建一個委託來填補我的tableView數據parserDidEndDocument調用後,我的委託不火的NSXMLParser加載

我的分析器類: DataPareser。^h

#import <Foundation/Foundation.h> 
#import "resturantModel.h" 


@protocol OnPareserDoneDelegate <NSObject> 
@required 
-(void) dataFormParser:(NSMutableArray *) itemsArray; 

@end 



@interface DataPareser : NSObject <NSXMLParserDelegate>{ 
    resturantModel * res; 


NSString * currentElementName ; 
NSString * currentString ; 


NSMutableString * name ; 
NSString * city ; 
NSString * street ; 

NSString * phone1 ; 
NSString * phone2 ; 
NSString * phone3 ; 
NSString * phone4 ; 
int ID; 

id<OnPareserDoneDelegate>onPareserDoneDelegate; 


} 

@property (nonatomic, assign) id onPareserDoneDelegate; 

- (void)loadUrl :(NSString *)url; 

@end 

DataPareser.m

import "DataPareser.h" 



@implementation DataPareser 

@synthesize onPareserDoneDelegate; 

static NSMutableArray * itemsArray; 




- (void)loadUrl:(NSString *)url{ 

NSURL* nsURL=[[NSURL alloc]initWithString:url]; 
NSXMLParser* parser=[[NSXMLParser alloc]initWithContentsOfURL:nsURL]; 

[parser setDelegate:self]; 

itemsArray=[[NSMutableArray alloc]init ]; 


BOOL success = [parser parse]; 

if(success) 
    NSLog(@"No Errors"); 
else 
    NSLog(@"Error Error Error!!!"); 

} 


- (void)parserDidEndDocument:(NSXMLParser *)parser{ 
//NSLog(@"Number of Resturants Found : %d",[itemsArray count]); 

if([itemsArray count]>0){ 
    NSLog(@"parserDidEndDocument"); 
    NSLog(@"%d",[itemsArray count]); 

    [[self onPareserDoneDelegate] dataFormParser:itemsArray]; 



}else { 

} 

[name release]; 
[city release]; 
[street release]; 

[phone1 release]; 
[phone2 release]; 
[phone3 release]; 
[phone4 release]; 


} 



@end 

,我用我的代表對我的ResultViewController

ResultViewController.h

#import "resturantModel.h" 
#import "SearchViewController.h" 
#import "DataPareser.h" 


@interface ResultsViewController : UIViewController <OnPareserDoneDelegate,UITableViewDelegate,UITableViewDataSource> { 
IBOutlet UINavigationController * navController; 
IBOutlet UITableView * table; 

NSMutableArray * array; 
resturantModel * res; 
NSMutableArray *items; 


} 

@property (nonatomic,retain)IBOutlet UINavigationController * navController; 
@property (nonatomic,retain)IBOutlet UITableView * table; 
@end 

ResultsViewController.m

#import "ResultsViewController.h" 


@implementation ResultsViewController 

@synthesize table; 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 

NSLog(@"initWithNibName"); 


self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
if (self) { 
    // Custom initialization 
} 
return self; 
} 

- (void)dealloc 
{ 

    [navController release]; 
    [super dealloc]; 
} 

- (void)didReceiveMemoryWarning 
{ 
// Releases the view if it doesn't have a superview. 
[super didReceiveMemoryWarning]; 

// Release any cached data, images, etc that aren't in use. 
} 

#pragma mark - View lifecycle 

- (void)viewDidLoad 
{ 
NSLog(@"Result Did Load"); 


table=[[UITableView alloc]init]; 

table.delegate=self; 

[table reloadData]; 

[self.view addSubview:navController.view]; 
[super viewDidLoad]; 
// Do any additional setup after loading the view from its nib. 
} 

- (void)viewDidUnload 
{ 
[super viewDidUnload]; 
// Release any retained subviews of the main view. 
// e.g. self.myOutlet = nil; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
// Return YES for supported orientations 
return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 


#pragma mark - 
#pragma mark OnPareserDoneDelegate deleget methodes 
-(void) dataFormParser:(NSMutableArray *) itemsArray { 
NSLog(@"dataFormParser"); 
} 



@end 

在我的應用程序,我調用分析器,它運行良好,但問題是,委託不火?!

回答

0

我看不到你在哪裏實際使用你的解析器類?

在某些時候,可能在ResultsViewController(取決於你是如何構建的東西),你會需要這樣的東西:

DataPareser *dataParser = [[DataPareser alloc] init]; // Create an instance of parser 
dataParser.onPareserDoneDelegate = self;    // Set the delegate to this class 
[dataParser loadUrl:urlToLoad];      // Start doing your work 

從源提供,這只是一個最好的猜測