好吧,我目前有一個TabBarController與五個選項卡。每個選項卡都是UINavigationControllers。與選項卡關聯的每個視圖鏈接到包含具有UIWebView的視圖的XIB文件。我想要發生的是當UIWebView中的鏈接被點擊時,一個新的導航視圖(帶有後退按鈕)被推入堆棧並且內容被點擊的鏈接填充,實際發生的事情是靠近但沒有雪茄。它載入我離開的原始頁面,例如:我在www.example.com上,點擊鏈接並加載新視圖(使用後退按鈕),然後重新加載www.example.com:(通過URL,通過UIWebView點擊,不正確
另外,我有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? – 2010-09-29 14:01:12
是的,我認爲這是我做錯了什麼。問題是......我不知道該怎麼做。 – SalsaSalsa 2010-09-29 14:04:37