2010-09-29 71 views
0

好吧,我目前有一個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; 
} 

}

+0

據我所知,你永遠不會傳遞URL到推送的新視圖控制器,所以怎麼可能知道要加載哪個URL? – 2010-09-29 14:01:12

+0

是的,我認爲這是我做錯了什麼。問題是......我不知道該怎麼做。 – SalsaSalsa 2010-09-29 14:04:37

回答

0

在webView:shouldStartLoadWithRequest 中,您絕不會將URL傳遞給您創建的「NavigationViewController」實例。它不能工作...

所以每次調用viewDidLoad時,都會根據tabBar狀態調用您的4個URL之一。

這裏是你應該做的:

在NavigationViewController:

  • 添加 「NSURL * PAGEURL」 屬性
  • 創建一個良好的init方法:
-(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]; 

然後正確初始化您的實例在web視圖:shouldStartLoadWithRequest

NavigationViewController *newView = [[NavigationViewController alloc] initWithURL:request.URL]; 

添加頂端

- (BOOL)webView的:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)請求導航類型:(UIWebViewNavigationType)導航nType {

NSURL * url = request.URL;

NSString * urlString = url.absoluteString;

如果([urlString isEqualToString:_pageURL.absoluteString]){返回YES;}

...

+0

我添加了你建議的附加代碼,然後在[webView loadRequest:_pageurl]上得到這個錯誤;行:警告:不兼容Objective-C類型'結構NSURL *',期望結構NSURLRequest *'當從不同的Objective-C類型傳遞參數1'loadRequest:'時 – SalsaSalsa 2010-09-29 14:17:16

+0

將NSURL的類型更改爲NSURLRequest只是使其崩潰。 – SalsaSalsa 2010-09-29 14:19:34

+0

使用你的大腦;-)只是創建一個與url的請求...我更新了我提供給你的代碼。你也將不得不改變第一次初始化... – Francescu 2010-09-29 14:31:53