首先咱們在HTML裏面定義一個標籤。html
<!-- 定義一個事件 把須要傳遞的視圖控制器的名字傳過去-->java
<a id="push" href="#" onclick="native.pushViewControllerWithTitle('CLDetailViewController','hello word');">web
你敢不敢點我</a>atom
咱們要獲取到這事件而且完成跳轉。lua
在控制器中咱們要這樣寫.net
#import <UIKit/UIKit.h>htm
#import <JavaScriptCore/JavaScriptCore.h>事件
@protocol TestJSExport <JSExport>ip
JSExportAs(calculateForJS,/** charlieCalculate 做爲 js方法的別名*/ - (void)calculateNumber:(NSNumber*)number);get
//跳轉頁面
- (void)pushViewController:(NSString*)viewControllerName withTitle:(NSString *)title;
@interface HomeViewController : UIViewController<TestJSExport,UIWebViewDelegate>
@property (nonatomic ,strong) UIWebView * myWebview;
@property (nonatomic ,strong) JSContext * context;
@end
////////////////
#pragma mark - UIWebViewDelegate
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
// 以 html title 設置 導航欄 title
self.title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
// Undocumented access to UIWebView's JSContext
self.context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
// 打印異常
self.context.exceptionHandler =
^(JSContext *context, JSValue *exceptionValue)
{
context.exception = exceptionValue;
NSLog(@"%@", exceptionValue);
};
// 以 JSExport 協議關聯 native 的方法
self.context[@"native"] = self;
// 以 block 形式關聯 JavaScript function
self.context[@"log"] =
^(NSString *str)
{
NSLog(@"%@", str);
};
}
//頁面的跳轉
-(void)pushViewController:(NSString *)viewControllerName WithTitle:(NSString *)title{
Class second = NSClassFromString(viewControllerName);
id secondVc = [[second alloc]init];
NSLog(@"%@",title);
((UIViewController*)secondVc).title = title;
[self.navigationController pushViewController:secondVc animated:YES];
}