将UIView加载成WevView(不懂的可以留言)

//+++++++++++++++++++++++这里是头文件使用++++++++++++++++++++++++
//#import <UIKit/UIKit.h>
//@class Help; //帮助页面
//@interface CZBKWebController : UIViewController
//@property (nonatomic, strong) Help *html; //帮助页面
//@end
//++++++++++++++++++++++++++++++++++++++++++++++++

@interface WebController () <UIWebViewDelegate>

@end

@implementation WebController

//载入UIWebView网页。  这里避免(用self.view)因为会造成死循环
- (void)loadView {
    UIWebView *wbView = [[UIWebView alloc] init];
    wbView.frame = [UIScreen mainScreen].bounds;
    self.view = wbView;
}

//载入视图
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 创建导航栏左侧的"关闭"按钮
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithTitle:@"关闭" style:UIBarButtonItemStylePlain target:self action:@selector(didClickCloseButton)];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    // 创建一个NSURL
    // 加载网页路径(访问网页时)
    //NSURL *url = [NSURL URLWithString:@"https://www.baidu.com"];
    
    // 加载本地路径self.html 自己定一个网页
    NSURL *url = [[NSBundle mainBundle] URLForResource:self.html.html withExtension:nil];
    
    // 创建一个请求对象【向网络发起请求的一个对象】
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    
    // 告诉UIWebView加载对应的"请求对象"
    UIWebView *webView = (UIWebView *)self.view;
    
    //代理使用
    webView.delegate = self;
    
    // 让webView加载指定的request请求对象
    [webView loadRequest:request];
}


//点击关闭按钮--关闭网页
- (void)didClickCloseButton {
    [self dismissViewControllerAnimated:YES completion:nil];
}


// WebView加载完毕网页后执行的代理方法
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    // 1. 编写要执行的js(JavaScript)代码
    NSString *jscode = [NSString stringWithFormat:@"document.location.href = '#%@';", self.html.ID];
    
    // 2. 让WebView执行上面的js代码
    [webView stringByEvaluatingJavaScriptFromString:jscode];
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值