您的位置:首页 > 产品设计 > UI/UE

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

2015-09-01 22:08 411 查看
//+++++++++++++++++++++++这里是头文件使用++++++++++++++++++++++++

//#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];

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: