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

Iphone 通过UIWebView单击传递URL不正确,iphone,uiwebview,uinavigationcontroller,Iphone,Uiwebview,Uinavigationcontroller,好的,我现在有一个有五个标签的TabBarController。每个选项卡都是UINavigationController。与选项卡关联的每个视图都链接到一个包含UIWebView视图的XIB文件。我想发生的是,当在UIWebView中单击一个链接时,一个带有后退按钮的新导航视图被推到堆栈上,内容将被单击的链接填充,实际发生的情况是关闭,但没有显示。它加载我离开的原始页面,例如:我在www.example.com上,我单击一个链接,然后用“后退”按钮加载新视图,然后重新加载www.example

好的,我现在有一个有五个标签的TabBarController。每个选项卡都是UINavigationController。与选项卡关联的每个视图都链接到一个包含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];
}


}

在webView中:shouldStartLoadWithRequest您永远不会将URL传递给您创建的NavigationViewController实例。它不能工作

因此,每次调用viewDidLoad时,根据选项卡状态调用4个URL中的一个

以下是您应该做的:

在NavigationViewController中:

添加NSURL*页面URL属性 创建一个好的init方法: 在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

添加到顶部

-BOOLwebView:UIWebView*webView应加载WithRequest:NSURLRequest*请求导航类型:UIWebViewNavigationTypenavigationType{

NSURL*url=request.url

NSString*urlString=url.absoluteString

如果[urlString IsequalString:_pageURL.absoluteString]{返回YES;}


据我所知,您从未将URL传递给正在被推送的新视图控制器,因此如何可能知道要加载的URL?是的,我认为这可能是我做错了什么。问题是……我不知道如何做。我添加了您建议的附加代码,然后在[webView loadRequest:\u pageurl]上出现此错误;行:警告:不兼容的Objective-C类型“struct NSURL*”,在传递“loadRequest:”的参数1时应为“struct NSURLRequest*”,从不同的Objective-C类型更改NSURL的类型为NSURLRequest只会使其崩溃。动动脑筋;-只需使用url创建一个请求……我更新了我提供给您的代码。您还必须使用C更改第一次初始化…您必须原谅我的无知我已经使用Obj-C和iPhone SDk不到一周了。而且,这创建了一个无限循环:对于viewDidLoad中的循环,将webView.delegate=self;放在[超级viewDidLoad]之前的末尾
-(id)initWithURL:(NSURL *)url
{
    if ([super initWithNibName:@"NavigationView" bundle:nil])
    {
       _pageURL = [url retain];
    }
    return self;
}
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];
NavigationViewController *newView = [[NavigationViewController alloc] initWithURL:request.URL];