0

我想从 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;

  }
4

1 回答 1

1

好吧,我复制并测试了你的代码,它运行良好,也许其他地方做错了......创建一个启用 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
于 2013-08-05T15:33:21.787 回答