webview返回按钮 iOS

来源:互联网 发布:残疾人产品设计 知乎 编辑:程序博客网 时间:2024/05/02 02:27

//

//  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 {

    [superviewDidLoad];

    // Do any additional setup after loading the view.

    [selfsetWebView];

    [selfaddReturnTool];

    [self request];

    

}


- (void)request

{

    //定好url

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

    

    //创建请求

    NSURLRequest *request = [NSURLRequestrequestWithURL:url];

    

    //发送请求给服务器

    [self.myWebloadRequest:request];

    

    

    

}


- (void)addReturnTool

{

    UIToolbar *toolBar = [[UIToolbaralloc]init];

    CGFloat toolBarX = 0;

    CGFloat toolBarY = 20;

    CGFloat toolBarW = [UIScreenmainScreen].bounds.size.width;

    CGFloat toolBarH = 50;

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

    toolBar.frame = toolBarFrame;

    [toolBar setBackgroundColor:[UIColorwhiteColor]];

    

    //toolbar添加按钮

    UIButton *leftButton = [[UIButtonalloc]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:[UIColorgrayColor]];

    //leftButton添加点击操作

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

    

    [toolBar addSubview:leftButton];

    

    

    _myToolBar = toolBar;

    [self.viewaddSubview:_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 = [[UIWebViewalloc]init];

    

    CGFloat webX = 0;

    CGFloat webY = 70;

    CGFloat webW = [UIScreenmainScreen].bounds.size.width;

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

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

    myWeb.frame = webFrame;

    _myWeb = myWeb;

    [self.viewaddSubview:_myWeb];

    

}

- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // 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


0 0
原创粉丝点击