我想从javascript调用目标c文件。
- (void)viewDidLoad
{
webview.delegate = self;
myButton.enabled = NO;
NSString *path=[[NSBundle mainBundle]pathForResource:@"1" ofType:@"html" inDirectory:@"files"];
NSURL *url=[NSURL fileURLWithPath:path];
NSURLRequest *request=[NSURLRequest requestWithURL:url];
[webview loadRequest:request];}
我正在使用此代码成功调用我的html页面,并使用以下代码调用目标c中的shouldStartLoadWithRequest方法。
<a href="didTap://button1"><img src="cercle24px.png" /></a>
现在我去调用新的TestViewController.m文件如何调用这个文件,我使用下面的代码。正确打印nslog并给出警告框。但是没有导航到下一个文件。请帮忙如果有人知道我,我正在等待您的宝贵回复。
- (BOOL)webView:(UIWebView*)webview shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
NSLog(@"what");
UIAlertView *tstAlert = [[UIAlertView alloc] initWithTitle:@"" message:@"Allowed only alphabets and numeric" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok",nil];
[tstAlert show];
NSString *absoluteUrl = [[request URL] absoluteString];
NSLog(@"absolute%@",absoluteUrl);
if ([absoluteUrl isEqualToString:@"didtap://button1"]) {
NSLog(@"yes");
TestViewController *testview=[[TestViewController alloc]initWithNibName:@"TestViewController" bundle:nil];
[self.navigationController pushViewController:testview animated:YES];
return NO;
}
NSLog(@"no");
return YES;
}
答案 0 :(得分:1)
好吧,我复制&amp;测试你的代码,它运作良好,也许在其他地方做错了... 在启用ARC的情况下创建一个新的“空模板”Xcode项目,将其粘贴到AppDelegat.m中:
//
// AppDelegate.m
// WebTest
//
// Created by Elf Sundae on 8/5/13.
// Copyright (c) 2013 www.0x123.com. All rights reserved.
//
#import "AppDelegate.h"
@interface SampleViewController : UITableViewController
@end
@implementation SampleViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"Sample Controller";
}
@end
#pragma mark -
@interface WebViewController : UIViewController <UIWebViewDelegate>
@end
@implementation WebViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UIWebView *web = [[UIWebView alloc] initWithFrame:self.view.bounds];
web.delegate = self;
[self.view addSubview:web];
[web loadHTMLString:@"<a href='didTap://button1'><img src='cercle24px.png' /></a>" baseURL:nil];
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType
{
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:nil message:@"alert message" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
NSString *urlString = request.URL.absoluteString;
if ([urlString caseInsensitiveCompare:@"didtap://button1"] == NSOrderedSame) {
#define __use_method 3 // it could be: 1/2/3
#if (__use_method == 1)
SampleViewController *controller = [[SampleViewController alloc] init];
[self.navigationController pushViewController:controller animated:YES];
#elif (__use_method == 2)
/* method 2 */
SampleViewController *controller = [[SampleViewController alloc] init];
[self.navigationController performSelector:@selector(pushViewController:animated:)
withObject:controller
withObject:@(YES)];
#elif (__use_method == 3)
/* method 3 */
__unsafe_unretained __typeof(self) _self = self;
double delayInSeconds = 0.01;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
SampleViewController *controller = [[SampleViewController alloc] init];
[_self.navigationController pushViewController:controller animated:YES];
});
#endif
return NO;
}
return YES;
}
@end
#pragma mark -
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window makeKeyAndVisible];
self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:
[WebViewController new]];
return YES;
}
@end