通过JS控制ViewController
2012-01-11 10:48
309 查看
JS与IOS原生应用程序看似没有联系,其实却暗藏玄机。
URL便是连接它们的桥梁,JS对应的是window.location,IOS对应的是NSURLRequest对象的mainDocumentURL.relativePath属性。
我做了一个Demo,看首页
底部是一个UITabBarController,它是window.rootViewController。UITabBarController的子层是一个UINavigationController,
中间是一个UIWebView,用来加载html文件。
文件系统:
关键代码:
AppDelegate.m代码
ViewController里包含UIWebView,它实现UIWebViewDelegate的方法
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
ViewController.m代码
index.html代码
点击按钮效果:
URL便是连接它们的桥梁,JS对应的是window.location,IOS对应的是NSURLRequest对象的mainDocumentURL.relativePath属性。
我做了一个Demo,看首页
底部是一个UITabBarController,它是window.rootViewController。UITabBarController的子层是一个UINavigationController,
中间是一个UIWebView,用来加载html文件。
文件系统:
关键代码:
AppDelegate.m代码
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; // Override point for customization after application launch. self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; _viewController.title = @"Home"; _viewController.tabBarItem.image = [UIImage imageNamed:@"house.png"]; _nav = [[[UINavigationController alloc] initWithRootViewController:_viewController] autorelease]; MoreViewController *moreViewCon = [[[MoreViewController alloc] init] autorelease]; moreViewCon.title = @"More"; moreViewCon.tabBarItem.image = [UIImage imageNamed:@"alarm.png"]; _tabBarController = [[[UITabBarController alloc] init] autorelease]; _tabBarController.viewControllers = [NSArray arrayWithObjects:_nav,moreViewCon,nil]; self.window.rootViewController = _tabBarController; [self.window makeKeyAndVisible]; return YES; }
ViewController里包含UIWebView,它实现UIWebViewDelegate的方法
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
ViewController.m代码
- (void)viewDidLoad { [super viewDidLoad]; NSString *path = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]; [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]]]; }
#pragma mark - webViewDelegate - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { //获取URL并且做比较,判断是否触发了JS事件,注意有"/" if ([request.mainDocumentURL.relativePath isEqualToString:@"/clicked"]) { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"you click me" message:@"clicked" delegate:nil cancelButtonTitle:@"sure" otherButtonTitles:@"cancel", nil]; [alertView show]; [alertView release]; SecondViewController *secondViewCon = [[[SecondViewController alloc] init] autorelease]; [self.navigationController pushViewController:secondViewCon animated:YES]; return false; } return true; }
index.html代码
<html> <head> <title>Home</title> <script> function jump() { var clicked=true; window.location="/clicked"; //改变URL 注意:要使用"/"分隔符 alert("js alert :jump"); } </script> </head> <body> <h1>Page One</h1> <button onclick="jump()">Click me</button> </body> </html>
点击按钮效果:
相关文章推荐
- 这篇为通过button控制跳转到ViewControllerPUSH 一个QQ好友列表
- [asp.net] 通过JS实现对treeview控件的复选框单选控制。
- js对文本框输入的字数限制、通过复选框控制其他html控件的可用性
- 通过iOS 9 SFSafariViewController提供完整的Web浏览体验
- 【译】MVC3 20个秘方-(2)通过脚手架自动生成controller和view ----代码先行/数据库先行
- c#操作通过button控制viewtree节点的改变
- 通过electron的webView使用按钮控制前进后退
- IOS开发—通过ChildViewController实现view的切换
- iOS_通过js获取webview源代码
- 通过UIActivityViewController实现更多分享服务
- 通过js控制input文本框输入文本类型
- Android中通过ViewHelper.setTranslationY实现View移动控制(NineOldAndroids开源项目)
- JS控制通过Select选择Gridview
- iOS-UI篇—UITabBarController控制View的生命周期(使用storyoard搭建
- Android 通过注入js实现读取webview中图片并放大的优化
- 通过按钮 启动NSTimer定时器 控制视图view移动效果
- 通过popToViewController跳到任意navigation的具体页面
- JS :通过上下箭头控制文本框中数字增减(KeyDown)
- 通过js控制时间,一秒一秒自己动
- 2017年第1贴:EXT.JS使用MVC模式时,注意如何协调MODEL, STORE,VIEW,CONTROLLER的关系