// ViewController.m // 12-掌握-webView #import "ViewController.h" @interface ViewController () <UIWebViewDelegate> @property (weak, nonatomic) IBOutlet UIWebView *webView; @property (weak, nonatomic) IBOutlet UIBarButtonItem *backItem; @property (weak, nonatomic) IBOutlet UIBarButtonItem *forward; @end @implementation ViewController - (IBAction)back:(id)sender { [self.webView goBack]; } - (IBAction)forward:(id)sender { [self.webView goForward]; } - (IBAction)refresh:(id)sender { [self.webView reload]; } - (void)viewDidLoad { [super viewDidLoad]; // Native(OC+Swift) + HTML5 self.webView.delegate = self; [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.baidu.com"]]]; } #pragma mark - <UIWebViewDelegate> - (void)webViewDidFinishLoad:(UIWebView *)webView { NSLog(@"%s", __func__); self.backItem.enabled = webView.canGoBack; self.forward.enabled = webView.canGoForward; } - (void)webViewDidStartLoad:(UIWebView *)webView { NSLog(@"%s", __func__); } - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error { NSLog(@"%s", __func__); self.backItem.enabled = webView.canGoBack; self.forward.enabled = webView.canGoForward; } @end
时间: 2024-10-13 22:24:42