gpt4 book ai didi

ios - 我想导航到 webview 中的另一个文件。

转载 作者:行者123 更新时间:2023-11-29 03:43:17 25 4
gpt4 key购买 nike

我想从 javascript 调用 Objective 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页面,并使用下面的代码调用 objective-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;

}

最佳答案

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

关于ios - 我想导航到 webview 中的另一个文件。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18060289/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com