Reputation: 846
I have some code inside the webViewDidFinishLoad delegate method, and I need some way to tell that this code has finished executing from within another method in a completely different part of my code. Is this possible? Basically what I want to do is something like this:
-(void) myMethod
{
// do stuff that causes webViewDidFinishLoad to be called
if (webViewDidFinishLoad has finished executing)
{
// do stuff with outputString
}
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
outputString = [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"myJSFunction('%@')",jsInputString]];
}
I looked at this answer already and it made no sense to me.
I should add that inside myMethod, I can be sure that webViewDidFinishLoad will only be called once.
Upvotes: 0
Views: 3065
Reputation: 18363
Try to avoid waiting for delegates and notifications in other methods - do the work that depends on them when they call back to your class. Don't call myMethod
until you get the delegate message. If myMethod
is called from user input, disable the associated controls until after the web view loads.
Upvotes: 1
Reputation: 2448
In webViewDidFinishLoad, you post a notification to your "myMethod" code part. Something like this:
in UIWebView:
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"webview_finished" object:self userInfo:nil];
}
in your "myMethod" code part:
- (void)viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myMethod) name:@"webview_finished" object:nil];
}
Upvotes: 0
Reputation: 5781
use a Boolean and set its value to YES
in the last line of webViewDidFinishLoad
.
then check in your myMethod
like this:
-(void) myMethod
{
// do stuff that causes webViewDidFinishLoad to be called
if (yourBoolean == YES)
{
// do more stuff
}
}
that should do your work.
Upvotes: 0
Reputation: 3956
Why not try GCD?
dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// WebView load content here
dispatch_async( dispatch_get_main_queue(), ^{
// load myMethod
});
});
Upvotes: 0
Reputation: 263
You can always set a boolean to indicate whether the webview has finished loading. And the value of the boolean will be changed in the webview delegate method:
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
loadFinished = YES;
}
Then in your own method, check "loadFinished" will do the task.
You have to set <UIWebViewDelegate>
for interface to use this delegate method
Upvotes: 2
Reputation: 8180
UIWebView
's iVar loading
webViewDidFinishLoad
.However, maybe you could reconsider your design and do in webViewDidFinishLoad
what to do after loading is finished.
Upvotes: 0