传递URL,通过UIWebView单击,不正确

时间:2010-09-29 13:51:43

标签: iphone uiwebview uinavigationcontroller

好的,我目前拥有的是一个带有五个标签的TabBarController。这些选项卡中的每一个都是UINavigationControllers。与选项卡关联的每个视图都链接到包含带有UIWebView的视图的XIB文件。我想要发生的是当在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;
}

}

1 个答案:

答案 0 :(得分: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];

然后在webView中正确初始化您的实例:shouldStartLoadWithRequest

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

添加到顶部

  

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)请求navigationType:(UIWebViewNavigationType)navigationType {

     

NSURL * url = request.URL;

     

NSString * urlString = url.absoluteString;

     

if([urlString isEqualToString:_pageURL.absoluteString]){return YES;}

     

...