在 webview 上添加活动指示器

发布于 2024-11-08 12:14:56 字数 4017 浏览 0 评论 0原文

我正在解析 RSS 提要并在网络视图中加载...我想要的是..将自定义活动指示器放置在确切的位置..解析开始的位置和解析结束的位置....下面是代码。

@implementation MenuAndWineListViewController

NSDictionary *dict;

UIAlertView * errorAlert;

- (void)viewDidLoad 
{
    self.title=@"Menu & WineList";  
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}


- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    activityIndicator1 = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]autorelease];

    activityIndicator1.frame=CGRectMake(0.0,0.0, 40.0, 40.0);

    activityIndicator1.center=self.view.center;

    [self.view addSubview:activityIndicator1];

    NSURL *baseURL=[[NSURL 

    URLWithString:@"http://www.riverstonechophouse.com.php5-22.dfw1-2.websitetestlink.com /?feedpages&max=0&sort_order=ASC&parent=12&child_of=12"]retain];

    NSURLRequest *request = [NSURLRequest requestWithURL:baseURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];

    connection1=[[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease];

    if ([stories count] == 0) {

        path = @"http://www.riverstonechophouse.com.php5-22.dfw1-2.websitetestlink.com/?feedpages&max=0&

        sort_order=ASC&parent=180&child_of=180";

        [self parseXMLFileAtURL:path];
    }

    [menuAndWineListViewController loadHTMLString:[dict objectForKey:@"description"]  baseURL:nil];

    [menuAndWineListViewController setClipsToBounds:YES];

     menuAndWineListViewController.opaque=NO;

     menuAndWineListViewController.backgroundColor=[UIColor clearColor];

    [menuAndWineListViewController setDelegate:self];
}


- (void)webViewDidStartLoad:(UIWebView *)webView
{
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
}


- (void)webViewDidFinishLoad:(UIWebView *)webView 
{
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}


- (void)parserDidStartDocument:(NSXMLParser *)parser
{   
    NSLog(@"found file and started parsing");   
}

- (void)parseXMLFileAtURL:(NSString *)URL
{   
    stories = [[NSMutableArray alloc] init];

    NSURL *xmlURL = [NSURL URLWithString:URL];

    rssParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];

    [rssParser setDelegate:self];

    [rssParser setShouldProcessNamespaces:NO];

    [rssParser setShouldReportNamespacePrefixes:NO];

    [rssParser setShouldResolveExternalEntities:NO];

    [rssParser parse];
}


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

    if ([elementName isEqualToString:@"item"]) 
    {
        item = [[NSMutableDictionary alloc] init];

        currentTitle = [[NSMutableString alloc] init];

        currentSummary = [[NSMutableString alloc] init];
    }
}


- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{     

    if ([elementName isEqualToString:@"item"]) 
    {
        [item setObject:currentTitle forKey:@"title"];
        [item setObject:currentSummary forKey:@"description"];
        [stories addObject:[item copy]];
    }

    for (i=0 ; i<stories.count;i++) 
    {
        dict = [stories objectAtIndex:i];
    }
}


- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{   
    if ([currentElement isEqualToString:@"title"]) 
    {
        [currentTitle appendString:string];
    }

    else if ([currentElement isEqualToString:@"description"]) 
    {
        [currentSummary appendString:string];
    } 
}


- (void)parserDidEndDocument:(NSXMLParser *)parser 
{
    [activityIndicator1 stopAnimating];

    [activityIndicator1 removeFromSuperview];

    NSLog(@"stories array has %d items", [stories count]);
}

i m parsing an rss feed and loading in a webview...wat i want is ..to place a custom activity indicator in the exact place..where the parsing begins and the place where the parsing ends....below is the code.

@implementation MenuAndWineListViewController

NSDictionary *dict;

UIAlertView * errorAlert;

- (void)viewDidLoad 
{
    self.title=@"Menu & WineList";  
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}


- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    activityIndicator1 = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]autorelease];

    activityIndicator1.frame=CGRectMake(0.0,0.0, 40.0, 40.0);

    activityIndicator1.center=self.view.center;

    [self.view addSubview:activityIndicator1];

    NSURL *baseURL=[[NSURL 

    URLWithString:@"http://www.riverstonechophouse.com.php5-22.dfw1-2.websitetestlink.com /?feedpages&max=0&sort_order=ASC&parent=12&child_of=12"]retain];

    NSURLRequest *request = [NSURLRequest requestWithURL:baseURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];

    connection1=[[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease];

    if ([stories count] == 0) {

        path = @"http://www.riverstonechophouse.com.php5-22.dfw1-2.websitetestlink.com/?feedpages&max=0&

        sort_order=ASC&parent=180&child_of=180";

        [self parseXMLFileAtURL:path];
    }

    [menuAndWineListViewController loadHTMLString:[dict objectForKey:@"description"]  baseURL:nil];

    [menuAndWineListViewController setClipsToBounds:YES];

     menuAndWineListViewController.opaque=NO;

     menuAndWineListViewController.backgroundColor=[UIColor clearColor];

    [menuAndWineListViewController setDelegate:self];
}


- (void)webViewDidStartLoad:(UIWebView *)webView
{
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
}


- (void)webViewDidFinishLoad:(UIWebView *)webView 
{
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}


- (void)parserDidStartDocument:(NSXMLParser *)parser
{   
    NSLog(@"found file and started parsing");   
}

- (void)parseXMLFileAtURL:(NSString *)URL
{   
    stories = [[NSMutableArray alloc] init];

    NSURL *xmlURL = [NSURL URLWithString:URL];

    rssParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];

    [rssParser setDelegate:self];

    [rssParser setShouldProcessNamespaces:NO];

    [rssParser setShouldReportNamespacePrefixes:NO];

    [rssParser setShouldResolveExternalEntities:NO];

    [rssParser parse];
}


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

    if ([elementName isEqualToString:@"item"]) 
    {
        item = [[NSMutableDictionary alloc] init];

        currentTitle = [[NSMutableString alloc] init];

        currentSummary = [[NSMutableString alloc] init];
    }
}


- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{     

    if ([elementName isEqualToString:@"item"]) 
    {
        [item setObject:currentTitle forKey:@"title"];
        [item setObject:currentSummary forKey:@"description"];
        [stories addObject:[item copy]];
    }

    for (i=0 ; i<stories.count;i++) 
    {
        dict = [stories objectAtIndex:i];
    }
}


- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{   
    if ([currentElement isEqualToString:@"title"]) 
    {
        [currentTitle appendString:string];
    }

    else if ([currentElement isEqualToString:@"description"]) 
    {
        [currentSummary appendString:string];
    } 
}


- (void)parserDidEndDocument:(NSXMLParser *)parser 
{
    [activityIndicator1 stopAnimating];

    [activityIndicator1 removeFromSuperview];

    NSLog(@"stories array has %d items", [stories count]);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

记忆之渊 2024-11-15 12:14:56

我将

[activityIndicator1 startAnimating];

将 放在parseXMLFileAtURL 方法的开头

,将放在 parserDidEndDocument 的开头。

[activityIndicator1 stopanimating];

像您一样

I'd put the

[activityIndicator1 startAnimating];

at the beginning of the parseXMLFileAtURL method

and the

[activityIndicator1 stopanimating];

at the beginning of the parserDidEndDocument like you did.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文