通过 UIWebView 点击传递 URL 不正确
Posted
技术标签:
【中文标题】通过 UIWebView 点击传递 URL 不正确【英文标题】:Passing URL, via UIWebView click, incorrectly 【发布时间】:2010-09-29 13:51:42 【问题描述】:好的,我目前拥有的是一个带有五个选项卡的 TabBarController。这些选项卡中的每一个都是 UINavigationControllers。与选项卡关联的每个视图都链接到一个 XIB 文件,该文件包含一个带有 UIWebView 的视图。我想要发生的是,当在 UIWebView 中单击一个链接时,一个新的导航视图(带有后退按钮)被推送到堆栈上,并且内容被单击的链接填充,实际发生的是关闭但没有雪茄。它加载了我离开的原始页面,例如:我在 www.example.com 上,然后单击一个链接,新视图加载(带有后退按钮),然后重新加载 www.example.com :(
另外,我在 viewDidLoad 方法中进行了检查,以确定选择了哪个选项卡,而哪个选项卡又告诉了需要显示哪些内容。
这是我的代码:
- (void)viewDidLoad
// Allows webView clicks to be captured.
webView.delegate = self;
// Places statsheet image centered into the top nav bar.
UIImage *image = [UIImage imageNamed: @"header.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
self.navigationItem.titleView = imageView;
[imageView release];
// Loads webpage according to the tab selected.
if (self.tabBarController.selectedIndex == 0)
[webView loadRequest: [ NSURLRequest requestWithURL: [ NSURL URLWithString: @"http://mobile.example.com" ] ] ];
else if (self.tabBarController.selectedIndex == 1)
[webView loadRequest: [ NSURLRequest requestWithURL: [ NSURL URLWithString: @"http://mobile.example.com/schedule" ] ] ];
else if (self.tabBarController.selectedIndex == 2)
[webView loadRequest: [ NSURLRequest requestWithURL: [ NSURL URLWithString: @"http://mobile.example.com/roster" ] ] ];
else if (self.tabBarController.selectedIndex == 3)
[webView loadRequest: [ NSURLRequest requestWithURL: [ NSURL URLWithString: @"http://mobile.example.com/stats" ] ] ];
else if (self.tabBarController.selectedIndex == 4)
[webView loadRequest: [ NSURLRequest requestWithURL: [ NSURL URLWithString: @"http://mobile.example.com/about" ] ] ];
// Loads the super class.
[super viewDidLoad];
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
NSURL *url = request.URL;
NSString *urlString = url.absoluteString;
NSRange page = [ urlString rangeOfString: @"/?page=" ];
NSRange post = [ urlString rangeOfString: @"/posts/" ];
NSRange notBlog = [ urlString rangeOfString: @"example" ];
// Allow webapge to load if next or prev button is clicked.
if ( page.location != NSNotFound )
return YES;
// Pass post link to new view with a back navigation button.
else if ( post.location != NSNotFound)
NavigationViewController *newView = [[NavigationViewController alloc] initWithNibName:@"NavigationView" bundle:nil];
// ^^^^^^^^^^^^^^^^^^ Here is where the new view is pushed but doesnt load the correct page
[self.navigationController pushViewController:newView animated:YES];
UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc]
initWithTitle:@"Back"
style:UIBarButtonItemStyleBordered
target:nil
action:nil];
self.navigationItem.backBarButtonItem = backBarButtonItem;
[backBarButtonItem release];
[newView release];
return NO;
// Allow all links that are a part of the five main pages to be loaded.
else if ( notBlog.location != NSNotFound)
return YES;
//Allows everything else to be loaded into a new view with a back navigation button.
else
NavigationViewController *newView = [[NavigationViewController alloc] initWithNibName:@"NavigationView" bundle:nil];
// ^^^^^^^^^^^^^^^^^^ Here is where the new view is pushed but doesnt load the correct page
[self.navigationController pushViewController:newView animated:YES];
UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc]
initWithTitle:@"Back"
style:UIBarButtonItemStyleBordered
target:nil
action:nil];
self.navigationItem.backBarButtonItem = backBarButtonItem;
[backBarButtonItem release];
[newView release];
return NO;
【问题讨论】:
据我所知,您永远不会将 URL 传递给正在推送的新视图控制器,那么它怎么可能知道要加载哪个 URL? 是的,我认为这很可能是我做错了。问题是……我不知道该怎么做。 【参考方案1】:在 webView:shouldStartLoadWithRequest 您永远不会将 URL 传递给您创建的“NavigationViewController”实例。它不能工作......
因此,每次调用 viewDidLoad 时,您都会根据 tabBar 状态调用 4 个 URL 之一。
这是你应该做的:
在 NavigationViewController 中:
添加“NSURL *pageURL”属性 创建一个好的初始化方法:-(id)initWithURL:(NSURL *)url if ([super initWithNibName:@"NavigationView" bundle:nil]) _pageURL = [url retain]; return self;
在 viewDidLoad 中:
if (_pageURL == nil)
// Loads webpage according to the tab selected.
if (self.tabBarController.selectedIndex == 0)
[webView loadRequest: [ NSURLRequest requestWithURL: [ NSURL URLWithString: @"http://mobile.example.com" ] ] ];
else if (self.tabBarController.selectedIndex == 1)
_pageURL = [ NSURL URLWithString: @"http://mobile.example.com/schedule" ];
else if (self.tabBarController.selectedIndex == 2)
_pageURL = [ NSURL URLWithString: @"http://mobile.example.com/roster" ];
else if (self.tabBarController.selectedIndex == 3)
_pageURL = [ NSURL URLWithString: @"http://mobile.example.com/stats" ];
else if (self.tabBarController.selectedIndex == 4)
_pageURL = [NSURL URLWithString:@"http://mobile.example.com/about" ];
[webView loadRequest:[NSURLRequest requestWithURL:_pageURL]];
// Loads the super class.
[super viewDidLoad];
然后在 webView:shouldStartLoadWithRequest 中正确初始化您的实例
NavigationViewController *newView = [[NavigationViewController alloc] initWithURL:request.URL];
添加到顶部
-(BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
NSURL *url = request.URL;
NSString *urlString = url.absoluteString;
if ([urlString isEqualToString:_pageURL.absoluteString])return YES;
...
【讨论】:
我添加了您建议的附加代码,然后在 [webView loadRequest:_pageurl];行:警告:从不同的 Objective-C 类型传递 'loadRequest:' 的参数 1 时,不兼容的 Objective-C 类型'struct NSURL *',预期的'struct NSURLRequest *' 将 NSURL 的类型改为 NSURLRequest 只会让它崩溃。 用你的大脑 ;-) 只需使用 url 创建一个请求......我更新了我提供给你的代码。您还必须更改第一次初始化... 您必须原谅我的无知,因为我使用 Obj-C 和 iPhone SDK 还不到一周左右的时间。此外,这创建了一个无限循环:( for the loop in viewDidLoad put "webView.delegate = self;"最后(就在 [super viewDidLoad] 之前)以上是关于通过 UIWebView 点击传递 URL 不正确的主要内容,如果未能解决你的问题,请参考以下文章
iOS - 通过 JavaScript 获取我在 UIWebView 中单击的图像的 URL