I have a UITableViewController (OnTVViewController) who's viewDidLoad is similar to below (basically parses some XML in the background and shows an activity indicator while this is happening).:
- (void)viewDidLoad {
OnTVXMLParser *xmlParser = [[OnTVXMLParser alloc] init];
/* Runs the parse command in the background */
[NSThread detachNewThreadSelector:@selector(parse) toTarget:xmlParser withObject:self];
//[xmlParser parse];
// new view to disable user interaction during downloading.
loadView = [[UIView alloc] initWithFrame: [[UIScreen mainScreen] bounds]];
loadView.backgroundColor = [UIColor darkGrayColor];
//Loader spinner
UIActivityIndicatorView *act = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[loadView addSubview:act];
act.center =loadView.center;
[self.view addSubview:loadView];
[self.view bringSubviewToFront:loadView];
[act startAnimating];
[act release];
[super viewDidLoad];
}
OnTVViewController also has this method to remove the activity indicator (just trying to log a message while debugging):
- (void)removeActivityView {
//[loadView removeFromSuperview];
NSLog(@"Should remove activity view here");
}
In my OnTVXMLParser class I have:
- (BOOL)parse{
NSAutorelease开发者_运维百科Pool *pool = [[NSAutoreleasePool alloc] init];
NSLog(@"Sleeping for 5 seconds");
[NSThread sleepForTimeInterval:5.0];
NSLog(@"Sleep finished");
// Simulated some elapsed time. I want to remove the Activity View
[self performSelectorOnMainThread:@selector(removeActivityView) withObject:nil waitUntilDone:false];
// Create and initialize an NSURL with the RSS feed address and use it to instantiate NSXMLParser
NSURL *url = [[NSURL alloc] initWithString:@"http://aurl.com/xml"];
NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
// Lots of parsing stuff snipped, this all runs fine
[pool release];
return YES;
}
Basically once the "parse" method on the XMLParser class has finished I want to call the removeActivityIndicator on the OnTVViewController object. It's probably really simple but I am new to iPhone programming and banging my head against the wall.
I understand I need to use performSelectorOnMainThread - but how do I reference the instance of OnTVViewController I want to target? I've imported the OnTVViewController header file into OnTVXMLParser.
At the moment I get the error:
-[OnTVViewController removeActivityView:]: unrecognized selector sent to instance 0x8840ba0'
Typically cocoa handles this with a delegate pattern. Basically add an ivar to the XML parser named delegate, and launching the parade set the delegate to self (the OnTVViewController), and then later use the delegate for all callbacks in the XML parser
The problem is that the selector called:
-[OnTVViewController removeActivityView:]: unrecognized selector sent to instance 0x8840ba0'
does not exist. You tell it to call this selector:
[self performSelectorOnMainThread:@selector(removeActivityView) withObject:nil waitUntilDone:false];
Which doesn't exist (note the :
), because your method is named:
- (void)removeActivityView
Try calling it
- (void)removeActivityView:(id)dummy
and see what happens.
You might also want to consider using NSNotificationCenter. This allows you to add an observer for a given key (a string) and selector, then post a notification from other parts of your application.
Examples:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(methodToCall:) name:@"SomeKeyName" object:nil];
[[NSNotificationCenter defaultCenter] postNotificationName:@"SomeKeyName" object:nil];
精彩评论