加载NSXMLParser后,我的委托不会触发

时间:2011-08-16 23:15:15

标签: iphone delegates

我有一个用作xml解析器的类,我试图创建一个委托,在parserDidEndDocument调用后用数据填充我的tableView,

我的解析器类: 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

在我的应用程序中,我正在调用解析器并且运行良好但代理不会触发的问题?!​​

1 个答案:

答案 0 :(得分: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

从提供的来源来看,这只是一个最好的猜测