performSelectorInBackground, notify other viewcontroller when done
I have a method used to save an image when the user clicks Save. I use performSelectorInBackground to save the image, the viewcontroller is popped and the previous viewcontroller is shown.
I want the table (on the previousUIViewController) to reload its d开发者_JAVA百科ata when the imagesaving is done.
How can I do this?
The save method is called like this:
[self performSelectorInBackground:@selector(saveImage) withObject:nil];
[self.navigationController popViewControllerAnimated:YES];
In your saveImage
method, post a notification just after finishing saving the image and before returning from the method. Something like this:
// post notification
[[NSNotificationCenter defaultCenter] postNotificationName:@"ImageSaved" object:nil];
In the controller handling the table, implement
- (void) imageSaved:(NSNotification *)notification{
[self.tableView reloadData];
}
and in its viewDidLoad
method add the following code to register for notifications:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(imageSaved:)
name:@"ImageSaved" object:nil];
finally, unregister in the dealloc
method adding
[[NSNotificationCenter defaultCenter] removeObserver:self];
I think the way to go is calling the method at the end of the saveImage routine. Maybe something like
[self performSelectorInBackground:@selector(saveImage) withObject:previousView];
And if you want to keep saveImage agnostic, create a protocol with a callback that your previousView can use.
@protocol processingFinishedDelegate
-(void)processingFinished;
@end
so At the end of saveImage you'll have:
[(id<processingFinishedDelegate>)object processingFinished];
and of course your previousView class interface should handle the delegate.
I am having problems using this to update UITextView with the approach "unforgiven" suggested. I tried couple of different ways but all failed... I also tried notifications + observers with this but no success... Why is that? It is working fine on UILabel but no UITextView with this message:
Tried to obtain the web lock from a thread other than the main thread or the web thread. This may be a result of calling to UIKit from a secondary thread. Crashing now...
精彩评论