【问题标题】:UIWebView - Trouble displaying current URL in text fieldUIWebView - 在文本字段中显示当前 URL 时出现问题
【发布时间】:2011-05-21 17:38:46
【问题描述】:

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

这是我添加到我的实现文件中的委托:

- (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。

标签: iphone objective-c ios uiwebview nsurlrequest


【解决方案1】:

改用- (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

【讨论】:

  • 我发现了哪里出错了。我必须将 添加到我的 .h 文件中,我忘记将我的 Web 视图连接到我的文件所有者中的委托。它现在可以工作了,但是打开 Web 视图的初始 URL 没有显示,只是空白,但之后单击的任何链接都会显示在文本字段中。因此,为了更正它,我必须更改为 [CODE]ViewDidFinishLoad[/CODE] 而不是 [CODE]ViewDidStartLoad[/CODE] 以获得 Web 视图启动时使用的初始 URL。
【解决方案2】:

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

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


}

【讨论】:

    猜你喜欢
    • 2020-06-17
    • 1970-01-01
    • 2012-03-23
    • 2011-01-01
    • 2020-12-11
    • 2014-10-12
    • 2011-09-12
    • 1970-01-01
    • 2023-04-05
    相关资源
    最近更新 更多