my delegate doesn't fire after NSXMLParser loaded
i have a class that work as xml parser , am trying to create a delegate to fill in my tableView with data after parserDidEndDocument called ,
my parser class : 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
and i used my delegate on my 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 cac开发者_运维知识库hed 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
in my app , am invoking the parser and it run well but the problem that the delegate doesn't fire ?!
I can't see where you are actually using your parser class?
At some point, possibly in ResultsViewController
(depending on how you are structuring things) you would need something like:
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
From the source provided this is only a best guess
精彩评论