iphone - NSXMLParser工作正常,但是如何实现UIActivityIndi​​cator来显示网络事件?

标签 iphone ios nsxmlparser

我从NSXMLParser将数据加载到UITableView中。
一切都很好,但是当应用程序加载XML数据时,我想实现的功能缺乏 Activity ,因此对用户来说,一切都变得更加友好。
我发现的一些示例仍然使我感到困惑,因为我没有像大多数人一样遵循XMLParser实现的方式。

我在这里提供数据,请提供一些代码示例,说明如何将其放入代码中。

XMLParser.h:

#import <UIKit/UIKit.h>

@class DAFAppDelegate, Stage, Month;

@interface XMLParser : NSObject <NSXMLParserDelegate>
{
     NSMutableString *currentElementValue;
     DAFAppDelegate *appDelegate;
     Stage *aStage;
     Month *aMonth;
}

- (XMLParser *) initXMLParser;
+ (NSDate *)dateFromString:(NSString *)dateString;
+ (NSString *)stringFromDate:(NSDate *)stringDate;

@end

XMLParser.m:
#import "XMLParser.h"
#import "DAFAppDelegate.h"
#import "Stage.h"
#import "Month.h"

@implementation XMLParser

- (XMLParser *) initXMLParser
{
     [super init];
     appDelegate = (DAFAppDelegate *)[[UIApplication sharedApplication] delegate];
     return self;
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
{
     if([elementName isEqualToString:@"Stages"])
     {
          //Initialize the array.
          appDelegate.stages = [[NSMutableArray alloc] init];
     }
     if([elementName isEqualToString:@"Month"])
     {
          //Initialize the Month.
          aMonth = [[Month alloc] init];
          aMonth.stagesPerMonth = [[NSMutableArray alloc] init];
          //Extract the attribute here.
          aMonth.name = [attributeDict valueForKey:@"name"];
          aMonth.monthID = [[attributeDict objectForKey:@"id"] integerValue];
          NSLog(@"Reading Month id value :%i", aMonth.monthID);
          NSLog(@"Reading Month name value :%@", aMonth.name);
     }
     if([elementName isEqualToString:@"Stage"])
     {
          //Initialize the Stage.
          aStage = [[Stage alloc] init];
          //Extract the attribute here.
          aStage.stageID = [[attributeDict objectForKey:@"id"] integerValue];
          NSLog(@"Reading id value :%i", aStage.stageID);
     }
     NSLog(@"Processing Element: %@", elementName);
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{ 
     if(!currentElementValue)
     {
          currentElementValue = [[NSMutableString alloc] initWithString:string];
     }
     else
     {
          [currentElementValue appendString:string];
     }
     NSLog(@"Processing Value: %@", currentElementValue);
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
     NSDate* date = [NSDate date];
     NSDateFormatter* nsformatter = [[[NSDateFormatter alloc] init] autorelease];
     [nsformatter setDateFormat:@"yyyy-MM-dd"];
     NSDate* stageDate = [XMLParser dateFromString:aStage.end];
     if([elementName isEqualToString:@"Stages"])
     {
          return;
     }
     if([elementName isEqualToString:@"Month"])
     {
          if (!aMonth.stagesPerMonth || aMonth.stagesPerMonth.count)
          {
               [appDelegate.stages addObject:aMonth];
          }
          [aMonth release];
          aMonth = nil;
     }
     if([elementName isEqualToString:@"Stage"])
     {
          NSTimeInterval interval = [date timeIntervalSinceDate:stageDate];
          if (interval < 0)
          {
               [aMonth.stagesPerMonth addObject:aStage];
          }
          [aStage release];
          aStage = nil;
     }
     else
     {
          [aStage setValue:currentElementValue forKey:elementName];
          [currentElementValue release];
          currentElementValue = nil;
     }
}

-(void) parserDidStartDocument:(NSXMLParser *)parser
{
     NSLog(@"parserDidStartDocument");
}

-(void) parserDidEndDocument: (NSXMLParser *)parser
{
     NSLog(@"parserDidEndDocument");
}

+ (NSDate *)dateFromString:(NSString *)dateString
{
     NSDateFormatter *nsDateFormatter = [[NSDateFormatter alloc] init];
     [nsDateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm"];
     NSDate *date = [nsDateFormatter dateFromString:dateString];
     return date;
     [nsDateFormatter release];
}

+ (NSString *)stringFromDate:(NSDate *)stringDate
{
     NSDateFormatter *stringDateFormatter = [[NSDateFormatter alloc] init];
     NSLocale *nlLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"nl_NL"];
     [stringDateFormatter setLocale:nlLocale];
     [stringDateFormatter setDateFormat:@"EEE', 'dd MMMM yyyy  HH:mma"];
     NSString *dateString = [stringDateFormatter stringFromDate:stringDate];
     return dateString;
     [stringDateFormatter release];
}

- (void) dealloc
{
     [aStage release];
     [aMonth release];
     [currentElementValue release];
     [super dealloc];
}

@end

DAFAppDelegate.h:
@class RootViewController;

@interface DAFAppDelegate : NSObject <UIApplicationDelegate>
{
     UIWindow *window;
     UINavigationController *navigationController;

     IBOutlet UITabBarController *rootTabController;

     RootViewController *rootViewController;
     NSMutableArray *stages;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UITabBarController *rootTabController;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
@property (nonatomic, retain) IBOutlet RootViewController *rootViewController;

@property (nonatomic, retain) NSMutableArray *stages;

+ (void) showAlert;

@end

DAFAppDelegate.m:
#import "DAFAppDelegate.h"
#import "RootViewController.h"
#import "XMLParser.h"


@implementation DAFAppDelegate

@synthesize window;
@synthesize navigationController;
@synthesize rootViewController;
@synthesize rootTabController;
@synthesize stages;

+ (void) showAlert
{
     UIAlertView *av = [[[UIAlertView alloc] initWithTitle:@"No Connection" message:@"Could not retrieve data" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
     [av show];
}

- (void)applicationDidFinishLaunching:(UIApplication *)application
{
     NSURL *url = [[NSURL alloc] initWithString:@"http://web.me.com/ijar/Stages.xml"];
     NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];

     //Initialize the delegate.
     XMLParser *parser = [[XMLParser alloc] initXMLParser];

     //Set delegate
     [xmlParser setDelegate:parser];

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

     if(success)
     {
          NSLog(@"No Errors");
     }
     else
     {
          [DAFAppDelegate showAlert];
          NSLog(@"Error Error Error!!!");
     }    
     [window addSubview:[rootTabController view]];
     [window makeKeyAndVisible];
}

- (void)dealloc
{
     [navigationController release];
     [rootViewController release];
     [rootTabController release];
     [window release];
     [stages release];
     [super dealloc];
}

@end

RootViewController.h:
@class DAFAppDelegate;

@interface RootViewController : UITableViewController
{
     DAFAppDelegate *appDelegate;
}

@end

RootViewController.m:
#import "RootViewController.h"
#import "DAFAppDelegate.h"
#import "DetailViewController.h"
#import "XMLParser.h"
#import "Stage.h"
#import "Month.h"
#import "AgendaCustomCell.h"


@implementation RootViewController

#pragma mark -
#pragma mark View lifecycle

- (void)viewDidLoad
{
     [super viewDidLoad];
     appDelegate = (DAFAppDelegate *)[[UIApplication sharedApplication] delegate];

     self.title = NSLocalizedString(@"Agenda", @"Master view navigation title");
     UIImageView *image=[[UIImageView alloc]initWithFrame:CGRectMake(0,0,45,45)] ;
     [image setImage:[UIImage imageNamed:@"topBarIcon.png"]];
     [self.navigationController.navigationBar.topItem setTitleView:image];
     self.tableView.backgroundColor = [UIColor clearColor];
}


#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [appDelegate.stages count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    Month *aMonth = [appDelegate.stages objectAtIndex:section];
    return aMonth.name;
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    Month *aMonth = [appDelegate.stages objectAtIndex:section];
    return [aMonth.stagesPerMonth count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     static NSString *CellIdentifier = @"AgendaCustomCell";

     AgendaCustomCell *cell = (AgendaCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

     if (cell == nil)
     {
          NSArray *topLevelObject = [[NSBundle mainBundle] loadNibNamed:@"AgendaCustomCell" owner:nil options:nil];

          for (id currentObject in topLevelObject)
          {
               if ([currentObject isKindOfClass:[UITableViewCell class]])
               {
                    cell = (AgendaCustomCell *)currentObject;
                    break;
               }
          }
     }

     Month *aMonth = [appDelegate.stages objectAtIndex:indexPath.section];
     Stage *aStage = [aMonth.stagesPerMonth objectAtIndex:indexPath.row];

     NSString *startDate = [XMLParser stringFromDate:[XMLParser dateFromString:aStage.start]];
     NSString *endDate = [XMLParser stringFromDate:[XMLParser dateFromString:aStage.end]];

     int endDateLength = endDate.length;
     NSString *dateTitle = [NSString stringWithFormat:@"%@ - %@", startDate, [endDate substringFromIndex:endDateLength -7]];

     cell.titleLabel.text = aStage.title;
     cell.dateLabel.text = dateTitle;
     cell.nameLabel.text = aStage.teacher;

     return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

#pragma mark -
#pragma mark Table view selection

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     //When a row is selected, create the detail view controller and set its detail item to the item associated with the selected row.
         DetailViewController *detailViewController = [[DetailViewController alloc] initWithStyle:UITableViewStyleGrouped];
         Month *aMonth = [appDelegate.stages objectAtIndex:indexPath.section];
     detailViewController.stage = [aMonth.stagesPerMonth objectAtIndex:indexPath.row];

         // Push the detail view controller.
         [[self navigationController] pushViewController:detailViewController animated:YES];
         [detailViewController release];
}

#pragma mark -
#pragma mark Memory management

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

@end

最佳答案

如果要在状态栏调用中使用标准 Activity 指示器:

[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];

在您调用解析器之前。

然后,在解析器完成之后(didEndDocument方法),调用:
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];

关于iphone - NSXMLParser工作正常,但是如何实现UIActivityIndi​​cator来显示网络事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5544474/

相关文章:

ios - NSXMLParser 问题

iphone - UIPickerView 高度不可更改

ios - UIMenuController 在 UILongGestureRecognizer 上不可见

iphone - 在 iPad 和 iPhone 上显示 UIwebview

javascript - 如何在 iOS Chrome 中使用 CSS scroll snap + picture bug 隐藏滚动条

ios - iPad 上的 Silverlight 支持吗?

ios - 我在后台线程中有一个 NSXMLParser。它是否也在后台调用其委托(delegate)方法?

ios - NSXMLParser\n 和\t 文本内部

iPhone - 是否可以覆盖静音模式或通过推送通知发出递归警报声?

iphone - 如何在右耳机上播放音频,在左耳机上播放音乐?