我在通過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
有人知道我在這裏做錯了嗎?謝謝。
我發現,我出了問題。我必須將添加到我的.h文件,並且忘記將我的Web視圖連接到我的文件所有者中的委託。它現在正在工作,但Web視圖打開時的初始URL不顯示,只是空白,但在文本字段中顯示的所有鏈接都已點擊。因此,爲了更正它,我必須更改爲「ViewDidFinishLoad」而不是「ViewDidStartLoad」,以便獲取Web視圖啓動的初始URL。 –
user754314
2011-05-21 20:38:13