【问题标题】:shouldStartLoadwithRequest not getting called in subclassshouldStartLoadwithRequest 没有在子类中被调用
【发布时间】:2011-10-03 01:04:22
【问题描述】:

我有一个视图控制的应用程序。在我的 xib 上,我没有 webviews,但我确实有按钮可以调出具有 webviews 的类。所以点击一个按钮,一个 uiwebview 会弹出,依此类推。

现在在我的一个课程中,我打开了一个远程网页,上面有一个链接。我想用 shouldStartLoadwithrequest 覆盖该按钮链接。这似乎永远不会被调用。

现在我的班级叫做 Tenth.m,我的视图控制器中有这段代码

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

NSLog(@" Test spot 1");
NSString* scheme = [[request URL] scheme];
if ([@"didTap" isEqual:scheme]) {
    // Call your method
     NSLog(@" Test spot 2");
    [self didTapButton1];
    return NO;
} else {
     NSLog(@" Test spot 3");
    return YES;
}}

但我什么也没得到(我的 NSLog 一个都没有)。我试过把它放到我的第十课中,但还是一无所获。我查看了其他示例,甚至下载了一个,但它仅使用 viewcontroller 和 delegates 类。没有三等舱。

我不知道为什么它没有被调用。

这是我的第三节课

#import "Tenth.h"


@implementation Tenth

- (void)awakeFromNib
{
[category10 loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.mywebsite.com/api/didtap.php"]]];

}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}

- (void)dealloc
{
[super dealloc];
}

- (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
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
 - (void)loadView
{
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{

 [super viewDidLoad];
}


- (void)viewDidUnload
{
[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 YES;
}

@end

远程页面代码

    <a href="didTap://button1">hello</a>

第十个.h文件

#import <UIKit/UIKit.h>


@interface Tenth : UIViewController <UIWebViewDelegate> {

IBOutlet UIWebView *category10;

}

-(IBAction)back;
@end

【问题讨论】:

  • 1.您将 webview 的委托设置为 self? 2.[@"didTap" isEqual:scheme]不应该是[@"didTap" isEqualToString:scheme]吗?

标签: iphone ios ipad


【解决方案1】:

我的第一个猜测是 Web 视图没有它的委托集。为了让它调用

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

包含此方法的类需要设置为 webview 的委托。所以在代码的某处应该有类似于webView.delegate = &lt;variable&gt; 的行,其中变量是实现shouldStartLoadWithRequest: 方法的类的实例。

当 webView 是 viewController 的视图的子视图时,这通常是“自我”。但是你描述你的应用程序的方式,我不确定它是否会是自我,但听起来它不会是自我,而是不同类的实例的名称。

【讨论】:

  • 所以我能找到的是 webview.delegate = self 进入第十个文件的 viewdidload(见上文)。但我得到了错误:使用未声明的标识符“webView”
  • 好的,好吧,使用 webView 视图变量的名称,不管它叫什么。
  • 不笑。但是我在哪里可以找到呢?在我的视图控制器中,我设置了 iboutlet Tenth *tenth;这就是我要找的吗?如果是这样,则会在“第十个”类型的对象上找不到属性委托的错误
  • 嗯,听起来 webView 是其他类的一部分。所以它应该是该类的属性或实例。因此,您需要找到在“.h”中看到的任何内容,如下所示: UIWebView *webView;说“webView”的部分是变量的名称,在您的情况下,它可能不是“webView”。无论变量的名称是什么,您都需要找到它并将变量的委托设置为具有 shouldStartLoadingRequest: 方法的任何类。
  • ok 在我的 Tenth.h 文件中,我设置了 set UIWebview *category10;所以现在我把 category10.delegate=self;在我的 Tenth.m 文件的 didLoad 中。并且 shouldStart.... 仍然没有被调用。 shouldStartLoadWithRequest 是否进入我的 viewcontroller、appdelegate 或 Tenth.m 文件?我现在在我的 viewcontroller.m 文件中有它。
【解决方案2】:

不妨试试这个:

- (void)viewDidLoad
{
 [super viewDidLoad];

  // Remember to set web view delegate to self.
  category10.delegate = self;
}

【讨论】:

    猜你喜欢
    • 2013-08-29
    • 1970-01-01
    • 1970-01-01
    • 2013-07-02
    • 1970-01-01
    • 2013-03-31
    • 1970-01-01
    • 1970-01-01
    • 2016-05-28
    相关资源
    最近更新 更多