IOS开发控件视图day10:webView网页链接转跳

1、绑定一个转跳按钮触发函数

-(void)login:(UIButton *)btn
{
    LoginViewController *lVC = [[LoginViewController alloc]init];//目标页面
    [self.navigationController pushViewController:lVC animated:YES];
}

2、设置WebView

@implementation LoginViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI]; 
	//返回按钮
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(16, 30, 20, 20)];
    [btn setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];     
}

//返回按钮触发事件
-(void)back:(UIButton *)btn
{
    [self.navigationController popViewControllerAnimated:YES];
}

//设置webView
-(void) initUI
{
    WKWebView *WebView = [[WKWebView alloc] init];
    WebView.UIDelegate = self;
    WebView.navigationDelegate = self;//设置两个代理
    [self.view addSubview:WebView];
    //约束
    [WebView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).with.offset(0);
        make.top.equalTo(self.view).with.offset(58);
        make.right.equalTo(self.view).with.offset(0);
        make.bottom.equalTo(self.view).with.offset(88);
    }];
    //设置转跳到的网址
    NSURL *url = [NSURL URLWithString:@"https://www.baidu.com/"];
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
    [WebView loadRequest:request];
}
//开始加载
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
    NSLog(@"开始加载网页");
}
//加载完成
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    NSLog(@"加载完成");
}
//加载失败
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error {
    NSLog(@"加载失败");
}
@end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值