Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Benjamin Jack
1,096 Pointsself.webView.delegate = self ?
In the UIWebViewDelegate video, this line of code is casually added to make the delegate work correctly. Can someone explain what's going on here?
From ViewController.m:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *path = [[NSBundle mainBundle] pathForResource:@"info" ofType:@"html"];
NSString *content = [NSString stringWithContentsOfFile:path encoding:NSASCIIStringEncoding error:nil];
NSURL *baseURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
[self.webView loadHTMLString:content baseURL:baseURL];
self.webView.delegate = self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark Web View Delegate
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[self.webView stringByEvaluatingJavaScriptFromString:@"showAlert()"];
}
2 Answers
Thiago Felix
2,081 PointsWhat is happening is that your view controller implements the webviewdelegate protocol, so, you need to tell the webview that you are his delegate. After that the webview control will call some methods in your view controller that are part of the delegate protocol.
Does it make sense? If not, this video extracted from one of storyboard's series of delegation may help you to understand
Benjamin Jack
1,096 PointsThat makes sense, thanks!