【问题标题】:working with UIWebViewDelegate使用 UIWebViewDelegate
【发布时间】:2012-08-20 21:21:17
【问题描述】:

我在 stackoverflow 上看到了很多关于 UIWebViewDelegates 的问题,但它们要么与我的问题不同,要么让我无法理解(或两者兼而有之)。

我的UIWebView 加载正常,但如果用户在加载后单击 web 视图中的链接,我希望能够采取某些操作。在viewDidLoad 中,我设置了self.webV.delegate = self;webV 是实例化GHWebView 的属性,我的UIWebView 类)。在视图控制器类中,我添加了以下代码,仅作为我开始使用实际方法之前的测试:

-(BOOL) webView:(GHWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if (navigationType==UIWebViewNavigationTypeLinkClicked)
{
    NSLog(@"Yes!");
}
else
{
    NSLog(@"No.");
}
return YES;
}

我想做的是得到“是的!”如果用户单击加载的 web 视图中的链接,则记录。但是当 webview 加载并且我点击其中的一个链接时,没有 NSLog。我应该怎么做?

(在此先感谢您的帮助以及您对还不了解代表的人的耐心等待......)

编辑:好的,这就是我认为 GHWebView 中的相关代码。 截至目前,GHWebView.h 中,我声明了属性requ (NSURLRequest)、conn (NSURLConnection) 和urlData (NSData) 以及一个方法-(void)loadWebViewWithbaseURLString:bus withURLString:us。然后在GHWebView.m我定义方法:

-(void)loadWebViewWithbaseURLString:bus withURLString:us
{
self.requ = [NSURLRequest requestWithURL:[NSURL URLWithString:us] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval: 20];
self.conn=[[NSURLConnection alloc] initWithRequest:self.requ delegate:nil];
NSError *error=nil;
NSURLResponse *resp=nil;
if (self.conn)
{
    self.urlData = [NSURLConnection sendSynchronousRequest: self.requ returningResponse:&resp error:&error];
    NSString *htmlString = [[NSString alloc] initWithData:self.urlData encoding:NSUTF8StringEncoding];
    [self loadHTMLString:htmlString baseURL:[NSURL URLWithString:bus]];
}
else
{
    UIAlertView *alert = [[UIAlertView alloc] init];
    alert.title = @"Unfortunately, I seem to be having a hard time connecting to the Internet.  Would you mind trying again later?  I'll make it worth your while, I promise.";
    [alert show];
}
}

我知道我必须声明一个委托并在我的头文件中添加一个协议——我认为这将涉及添加类似的代码

@property (nonatomic, assign) id<UIWebViewDelegate> delegate;

@protocol GHWebViewDelegate <UIWebView>
-(void)loadWebViewWithbaseURLString:bus withURLString:us;
@end

但是当我回到实现文件并尝试用self.delegates 方法-(void)loadWebViewWithbaseURLString:bus withURLString:us 替换selfs 时,我得到property not found on object of type 错误。我该怎么办?

【问题讨论】:

    标签: objective-c xcode ios5


    【解决方案1】:

    您提到您正在将委托设置为负责实例化 GHWebView 类的 ViewController。虽然这很好,但我的问题是你的 GHWebView 类中有一个部分,你将这个对象分配为 UIWebView 的代表,以便它可以响应 -(BOOL)webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType 方法,然后调用 webView:(GHWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType这样当这个协议方法被调用时,你的委托(在你的情况下是你的 ViewController)可以参加电话会议?

    【讨论】:

    • 不,我没有!到目前为止,在我非常有限的经验中,我只在父类而不是子类中分配了代表,所以我有点不知所措——我知道一点,但是,正如我们和亚历山大·波普所知,这是一个危险的事物。我将编辑我的问题,向您展示我所知道的或我认为我知道的,希望您能纠正我的错误并填补我理解中的空白。非常感谢您的帮助。
    • 我从您添加到问题的代码中注意到的一件事是:您的名为 delegate 的属性是 UIWebView 类型,您想要的是 GHWebView 类型,因为它将是负责此类协议的委托。你可能想看看这个问题:stackoverflow.com/questions/626898/…
    • 非常感谢——感谢您注意到您注意到的内容并为我指出了那个有用的问题/答案!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-18
    • 2015-02-23
    • 1970-01-01
    • 2012-04-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多