UIWebView - 在文本字段中显示当前URL时出错

问题描述:

我在通过URL请求获取当前URL并将其传递到我的Web视图上方的文本字段时遇到问题。我试着记录它,它返回NULL,并且文本字段保持空白。UIWebView - 在文本字段中显示当前URL时出错

这是我加入到我的执行文件中的委托:

- (void)webViewDidStartLoad:(UIWebView *)webView { 
    NSURL* url = [webView.request URL]; 
    _webAddress.text = [url absoluteString]; 
} 

,这里是我的WebViewController.h文件:

#import <UIKit/UIKit.h> 


@interface WebViewController : UIViewController { 

    IBOutlet UIWebView *_webView; 
    IBOutlet UITextField *_webAddress; 
    IBOutlet UIActivityIndicatorView *activityIndicator; 
    NSTimer *timer; 
    NSString *_webURL; 
} 

@property (nonatomic, retain) IBOutlet UIWebView *webView; 
@property (nonatomic, retain) IBOutlet UITextField *webAddress; 
@property (nonatomic, retain) NSString *webURL; 

- (IBAction) doneButton:(id)sender; 


@end 

,这里是我的WebViewController.m文件:

#import "WebViewController.h" 


@implementation WebViewController 

@synthesize webView = _webView; 
@synthesize webAddress = _webAddress; 
@synthesize webURL = _webURL; 


// Dismiss WebViewController 
- (IBAction) doneButton:(id)sender { 

    [[self parentViewController] dismissModalViewControllerAnimated:YES];   
} 


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 


- (void)didReceiveMemoryWarning 
{ 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 

#pragma mark - View lifecycle 

- (void)webViewDidStartLoad:(UIWebView *)webView { 
    NSURL* url = [webView.request URL]; 
    _webAddress.text = [url absoluteString]; 
} 

- (void)viewDidLoad 
{ 
    [_webView addSubview:activityIndicator]; 
    [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:_webURL]]]; 
    timer = [NSTimer scheduledTimerWithTimeInterval:(1.0/2.0) target:self selector:@selector(loading) userInfo:nil repeats:YES]; 

    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 
} 

- (void)loading { 
    if (!_webView.loading) 
     [activityIndicator stopAnimating]; 

     else 
      [activityIndicator startAnimating]; 
} 

- (void)viewDidUnload 
{ 
    _webURL = nil; 
    _webView = nil; 
    _webAddress = nil; 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    // Return YES for supported orientations 
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
} 


- (void)dealloc 
{ 
    [_webURL release]; 
    [_webView release]; 
    [_webAddress release]; 
    [super dealloc]; 
} 


@end 

有人知道我在这里做错了吗?谢谢。

+0

我发现,我出了问题。我必须将添加到我的.h文件,并且忘记将我的Web视图连接到我的文件所有者中的委托。它现在正在工作,但Web视图打开时的初始URL不显示,只是空白,但在文本字段中显示的所有链接都已点击。因此,为了更正它,我必须更改为“ViewDidFinishLoad”而不是“ViewDidStartLoad”,以便获取Web视图启动的初始URL。 – user754314 2011-05-21 20:38:13

改为使用- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType委托。这会给你实际的要求。

http://developer.apple.com/library/ios/#documentation/uikit/reference/UIWebViewDelegate_Protocol/Reference/Reference.html#//apple_ref/doc/uid/TP40006951-CH3-SW6

+0

我发现我出错的地方。我必须将添加到我的.h文件,并且忘记将我的Web视图连接到我的文件所有者中的委托。 它现在正在工作,但Web视图打开的初始URL不显示,只是空白,但在文本字段中显示的任何链接之后单击。 所以要更正它,我必须更改为[CODE] ViewDidFinishLoad [/ CODE],而不是[CODE] ViewDidStartLoad [/ CODE],以便获取Web视图启动的初始URL。 – user754314 2011-05-21 20:37:20


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

NSURL* url = [request URL]; 
_webAddress.text = [url absoluteString]; 


}