首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用UIWebViewDelegate

使用UIWebViewDelegate
EN

Stack Overflow用户
提问于 2012-08-20 21:21:17
回答 1查看 260关注 0票数 1

我在堆栈溢出上看到了许多关于UIWebViewDelegates的问题,但它们要么是关于与我的问题不同的问题,要么是我无法理解(或者两者都理解)的问题。

我的UIWebView加载得很好,但我想在加载后用户单击want视图中的链接时,能够采取特定的操作。在viewDidLoad中,我设置了self.webV.delegate = self; (webV是实例化GHWebView,我的UIWebView类)的属性。在视图控制器类中,我添加了以下代码,仅作为开始实际方法之前的测试:

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

我想做的是得到“是的!”如果用户单击加载的webview中的链接,则登录。但是当webview加载并单击其中的链接时,就没有NSLog了。我该怎么做才好呢?

(预先感谢你的帮助和你对还不了解代表的人的耐心……)

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

代码语言:javascript
复制
-(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];
}
}

我知道我必须声明一个委托,还必须向我的头文件中添加一个协议--我认为这需要添加如下代码

代码语言:javascript
复制
@property (nonatomic, assign) id<UIWebViewDelegate> delegate;

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

但是,当我回到实现文件并试图在方法self中用self.delegates替换property not found on object of type s时,我会得到property not found on object of type错误。我该怎么办?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-08-21 03:37:06

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12045382

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档