【发布时间】: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