webview返回按钮 iOS

//

//  myWebViewController.m

//  webView

//

//  Created by wusiping on 15/11/22.

//  Copyright (c) 2015 wusiping. All rights reserved.

//


#import "myWebViewController.h"


@interface myWebViewController ()

@property(nonatomic, weak) UIWebView *myWeb;

@property(nonatomic, weak) UIToolbar *myToolBar;

@property(nonatomic, copy) NSString *currentUrl;



@end


@implementation myWebViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    [self setWebView];

    [self addReturnTool];

    [self request];

    

}


- (void)request

{

    //定好url

    NSURL *url = [NSURL URLWithString:@"http://m.baidu.com"];

    

    //创建请求

    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    

    //发送请求给服务器

    [self.myWeb loadRequest:request];

    

    

    

}


- (void)addReturnTool

{

    UIToolbar *toolBar = [[UIToolbar alloc]init];

    CGFloat toolBarX = 0;

    CGFloat toolBarY = 20;

    CGFloat toolBarW = [UIScreen mainScreen].bounds.size.width;

    CGFloat toolBarH = 50;

    CGRect toolBarFrame = CGRectMake(toolBarX, toolBarY, toolBarW, toolBarH);

    toolBar.frame = toolBarFrame;

    [toolBar setBackgroundColor:[UIColor whiteColor]];

    

    //toolbar添加按钮

    UIButton *leftButton = [[UIButton alloc]init];

    CGFloat leftButtonX = 25;

    CGFloat leftButtonY = 5;

    CGFloat leftButtonW = 50;

    CGFloat leftButtonH = 30;

    CGRect leftButtonFrame = CGRectMake(leftButtonX, leftButtonY, leftButtonW, leftButtonH);

    leftButton.frame = leftButtonFrame;

    [leftButton setTitle:@"返回" forState:UIControlStateNormal];

    [leftButton setBackgroundColor:[UIColor grayColor]];

    //leftButton添加点击操作

    [leftButton addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];

    

    [toolBar addSubview:leftButton];

    

    

    _myToolBar = toolBar;

    [self.view addSubview:_myToolBar];

    

}


- (void)goBack

{

    // 判断是否可以返回,如果可以,就返回

    if (_myWeb.canGoBack) {

        _currentUrl = _myWeb.request.URL.absoluteString;

        NSLog(@"%@",_currentUrl);

        

        //判断当前url 是否== 某个连接,如果等于就执行某个操作(比如直接返回主页)

        if ([_myWeb.request.URL.absoluteString  isEqual: @"http://m.baidu.com"]) {

            //返回主页相关代码

        }

        [_myWeb goBack];

    }

}


- (void)setWebView

{

    UIWebView * myWeb = [[UIWebView alloc]init];

    

    CGFloat webX = 0;

    CGFloat webY = 70;

    CGFloat webW = [UIScreen mainScreen].bounds.size.width;

    CGFloat webH = [UIScreen mainScreen].bounds.size.height - 49;

    CGRect webFrame = CGRectMake(webX, webY, webW, webH);

    myWeb.frame = webFrame;

    _myWeb = myWeb;

    [self.view addSubview:_myWeb];

    

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


/*

#pragma mark - Navigation


// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/


@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值