UIWebView未显示已解析的XML数据

时间:2011-10-24 09:21:48

标签: objective-c ios uiwebview

我有一个标签<AboutUs>(有HTML标签),我已将字符串部分存储在本地字符串中。现在我想将它传递给我的webView进行显示,但它显示了我的字符串(null)。

这是我的代码,感谢任何帮助:

@class AppDelegate_iPhone;
@interface AboutUsViewController : UIViewController<NSXMLParserDelegate> {

    NSMutableString *aboutUsString;
    NSString *currentElement;

    IBOutlet UIWebView *webView;
    AppDelegate_iPhone *appDelegate;
}

@property(nonatomic,retain) NSMutableString *aboutUsString;
@property(nonatomic,retain) IBOutlet UIWebView *webView;
-(id)init;

@end



@implementation AboutUsViewController
@synthesize aboutUsString,webView;


-(id)init{
    if(self == [super init]){
        aboutUsString = [[NSMutableString alloc]init];
    }
    return self; 
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];

    appDelegate = (AppDelegate_iPhone *)[[UIApplication sharedApplication] delegate];

    NSURL *url = [[NSURL alloc] initWithString:@"http://mobileecommerce.site247365.com/admin/AboutUs.xml"];
    NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];

    //Initialize the delegate.
    AboutUsViewController *parser = [[AboutUsViewController alloc] init];

    //Set delegate
    [xmlParser setDelegate:parser];

    //Start parsing the XML file.
    BOOL success = [xmlParser parse];

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

    NSLog(@"After Parsing=== = = = = = = = == = = = = %@",appDelegate.TextString);



}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewDidUnload {
    [super viewDidUnload];
}

- (void)parserDidStartDocument:(NSXMLParser *)parser
{

}


- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    currentElement = elementName;

    if([currentElement isEqualToString:@"AboutUs"]) {

    }

}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    if([currentElement isEqualToString:@"AboutUs"]) {

        NSMutableString *outputBuilder =  [[NSMutableString alloc]init] ;

        [outputBuilder appendString:[NSString stringWithFormat:@"%@", self.aboutUsString]];

        [outputBuilder appendString:[NSString stringWithFormat:@"%@", string]];

        self.aboutUsString = outputBuilder;

        [outputBuilder release];

    }
    else 
    {
        self.aboutUsString = string;
    }



}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if([elementName isEqualToString:@"AboutUs"]) {

        [webView setScalesPageToFit:YES];
        [webView loadHTMLString:self.aboutUsString baseURL:[NSURL URLWithString:@"http://www.hitchhiker.com/message"]];

        NSLog(@"In DID End Element ===== %@",aboutUsString);

        appDelegate.TextString = [[NSMutableString alloc]initWithString:aboutUsString];

    }

    currentElement = @"";
}



- (void)parserDidEndDocument:(NSXMLParser *)parser
{
    NSLog(@"In Document End =========  %@",aboutUsString);

    NSLog(@"Appdelgate Text String %@",appDelegate.TextString);


}

- (void)dealloc {
    [super dealloc];
    [webView release];
}


@end

1 个答案:

答案 0 :(得分:2)

viewDidLoad方法中,您正在创建 new AboutUsViewController,然后获取所有NSXMLParser委托调用。它有自己的Web视图,一个将加载已解析的HTML,但您永远不会看到它,因为解析视图控制器实际上永远不可见。

您应该将self设置为xml解析器的delegate,而不是创建新实例。