实现的功能:这个小demo的基本功能是在文本区输入网址,点击确定按钮后加载网页。加载中显示旋转轮。以下直接给出代码,重要部分加上了注释。
代码实现:
.h文件:
#import
@interface ViewController : UIViewController<UIWebViewDelegate>
@property (strong , nonatomic) UITextField *addr;//输入网址
@property (strong , nonatomic) UIWebView *webView;//网页视图
@property (strong , nonatomic) UIButton *button;//确定按钮
@end
.m文件:
#import "ViewController.h"
@interface ViewController ()
{
}
@end
@implementation ViewController
{
UIActivityIndicatorView *_activityIndicator;//旋转进度轮
}
- (void)viewDidLoad
{
[super viewDidLoad];
//设置按钮
self.button = [UIButton buttonWithType:UIButtonTypeRoundedRect];//设置按钮圆角
self.button.frame = CGRectMake(310, 63, 80, 30);//设置按钮尺寸
self.button.backgroundColor = [UIColor grayColor];
[self.button setTitle:@"确定" forState:UIControlStateNormal];
[self.button addTarget:self action:@selector(goTapped:) forControlEvents:UIControlEventTouchDown];
//设置文本区
self.addr = [[UITextField alloc] initWithFrame:CGRectMake(20, 60, [UIScreen mainScreen].bounds.size.width*2/3, 35)];
self.addr.backgroundColor = [UIColor whiteColor];
self.addr.alpha = 1.0;//透明度
self.addr.textColor = [UIColor redColor];
self.addr.clearsOnBeginEditing = YES;//开始编辑时清空文本区域
self.addr.placeholder = @"请输入网址";//占位字符串
self.addr.font = [UIFont boldSystemFontOfSize:25.0f];//字体加粗25号
self.addr.textAlignment = NSTextAlignmentCenter;//居中
self.addr.borderStyle = UITextBorderStyleRoundedRect;//文本框样式设置
self.addr.secureTextEntry = NO;//密码圆点输入
self.addr.autocorrectionType = YES;//设置是否启动自动提醒更新功能
self.addr.returnKeyType = UIReturnKeyDefault;//设置弹出的键盘带形式与带的按键
self.addr.keyboardAppearance = UIKeyboardAppearanceLight;//设置弹出的键盘带外观
//设置网页视图
self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 160, [UIScreen mainScreen].bounds.size.width, 550)];
self.webView.scalesPageToFit = YES;//调整页面适应大小
self.webView.delegate = self;
_activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 32.0f, 32.0f)];
[_activityIndicator setCenter:self.view.center];//设置为中心
_activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;//样式设置
_activityIndicator.hidden = YES;//隐藏
//统计组件到视图
[self.view addSubview:_activityIndicator];
[self.view addSubview:self.button];
[self.view addSubview:self.webView];
[self.view addSubview:self.addr];
}
//webview开始加载
- (void)webViewDidStartLoad:(UIWebView *)webView API_DEPRECATED("No longer supported.", ios(2.0, 12.0))
{
_activityIndicator.hidden = NO;//显示旋转盘
[_activityIndicator startAnimating];//开始动画
}
//结束加载
- (void)webViewDidFinishLoad:(UIWebView *)webView API_DEPRECATED("No longer supported.", ios(2.0, 12.0))
{
[_activityIndicator stopAnimating];//停止动画
_activityIndicator.hidden = YES;//隐藏
}
//加载失败
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error API_DEPRECATED("No longer supported.", ios(2.0, 12.0))
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:[error localizedDescription] delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
[alert show];//警告提示
}
//监听方法
- (IBAction)goTapped:(id)sender
{
[self.addr resignFirstResponder];
NSString *reqAddr = self.addr.text;
if(![reqAddr hasPrefix:@"http://"]) {
reqAddr = [NSString stringWithFormat:@"http://%@",reqAddr];
self.addr.text = reqAddr;
}//加网址头
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:reqAddr]];//根据地址生成请求
[self.webView loadRequest:request];//加载网页
}
@end
重要逻辑梳理:
初始化的部分,先实例化视图,然后设置为自适应,并把控制器设置为它的代理,因为源码中存在:@property (nullable, nonatomic, assign) id delegate;一段代理属性。它需要持有控制器的引用,这样才能调用控制器的中实现的UIWebView的方法。最后是显示到视图上,它是一个UIView。
self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 160, [UIScreen mainScreen].bounds.size.width, 550)];
self.webView.scalesPageToFit = YES;
self.webView.delegate = self;
[self.view addSubview:self.webView];
监听方法中,先使得文本区失去焦点,再确保添加http://头;然后通过网址字符串得到URL对象;再通过URL对象得到URL请求实例;最终,调用webview的加载请求方法。就可以显示在视图中了。
[self.addr resignFirstResponder];
NSString *reqAddr = self.addr.text;
if(![reqAddr hasPrefix:@"http://"]) {
reqAddr = [NSString stringWithFormat:@"http://%@",reqAddr];
self.addr.text = reqAddr;
}
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:reqAddr]];
[self.webView loadRequest:request];