【发布时间】:2024-01-15 10:39:01
【问题描述】:
好的,我目前拥有的是一个带有五个选项卡的 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?
-
是的,我认为这很可能是我做错了。问题是……我不知道该怎么做。
标签: iphone uiwebview uinavigationcontroller