webView(简单的浏览器)
2015-12-20 00:30
405 查看
1 #import "MJViewController.h" @interface MJViewController () <UISearchBarDelegate, UIWebViewDelegate> @property (weak, nonatomic) IBOutlet UIWebView *webView; @property (weak, nonatomic) IBOutlet UIBarButtonItem *backButton; @property (weak, nonatomic) IBOutlet UIBarButtonItem *forwarButton; @end @implementation MJViewController - (void)viewDidLoad { [super viewDidLoad]; [self loadString:@"http://m.baidu.com"]; } // 让浏览器加载指定的字符串,使用m.baidu.com进行搜索 - (void)loadString:(NSString *)str { // 1. URL 定位资源,需要资源的地址 NSString *urlStr = str; if (![str hasPrefix:@"http://"]) { urlStr = [NSString stringWithFormat:@"http://m.baidu.com/s?word=%@", str]; } NSURL *url = [NSURL URLWithString:urlStr]; // 2. 把URL告诉给服务器,请求,从m.baidu.com请求数据 NSURLRequest *request = [NSURLRequest requestWithURL:url]; // 3. 发送请求给服务器 [self.webView loadRequest:request]; } #pragma mark - 搜索栏代理 // 开始搜索 - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar { NSLog(@"%@", searchBar.text); [self loadString:searchBar.text]; [self.view endEditing:YES]; } // 文本改变 - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { NSLog(@"searchText - %@", searchText); } #pragma mark - WebView代理方法 #pragma mark 完成加载,页面链表数据会更新 - (void)webViewDidFinishLoad:(UIWebView *)webView { // 根据webView当前的状态,来判断按钮的状态 self.backButton.enabled = webView.canGoBack; self.forwarButton.enabled = webView.canGoForward; } @end
相关文章推荐
- iOS Default-568@2x.png启动图片设置问题
- 【Swift】通过tag删除动态创建的UIButton
- CocoaPod的安装及与Swift的桥接
- Android Studio使用lambda
- iOS LBS
- Android设置虚线、圆角、渐变
- android自定义滑动开关控件,适合所有的android系统 《转载》
- iOS开发之蓝牙通讯
- 图文详解Unity3D中Material的Tiling和Offset是怎么回事
- android数据存储的选择
- Android之JNI的使用
- 结合volley实现图片三级缓存
- swift控件之旅之UIButton
- Unity日志工具——封装,跳转
- iOS军火库-好用的ActionSheetView
- POJ3321 Apple Tree
- 记录Android学习之路
- applicationIconBadgeNumber和localNotification申请通知弹出
- 加速Gradle构建项目
- 报错:Could not find method android.view.ViewTreeObserver.removeOnGlobalLayoutListener