2012-01-25 101 views
0

在我的应用程序中,我使用TapDetectingWindow来检测UIWebView上的触摸(完全按照http://mithin.in/2009/08/26/detecting-taps-and-events-on-uiwebview-the-right-way上的详细说明)。 我用它在一个视图控制器与在头文件中的参考建议,并在执行文件中像WebView上的TapDetectingWindow无法正常工作

- (id)init { 
    self = [super init]; 
    if (self) { 
      tapDetectingWindow = (TapDetectingWindow *)[[UIApplication sharedApplication].windows objectAtIndex:0];            //mWindow, variable reference to TapDetectingWindow 
     tapDetectingWindow.viewToObserve = webView; 
     tapDetectingWindow.controllerThatObserves = self;  

     webView = [[UIWebView alloc] init]; 
     [webView setFrame:CGRectMake(0, 0, 340, 1900)]; 
     [webView setTag:1]; 
     [webView addSubview:keyboardText]; 
     [webView setUserInteractionEnabled:NO]; 
     [webView setDelegate:self]; 
     [webView setOpaque:0.0]; 

     [webView loadHTMLString:htmlString baseURL:NULL]; 
     [self.view addSubview:webView]; 

     keyboardText = [[UITextField alloc] init]; 
     keyboardText.autocorrectionType = UITextAutocorrectionTypeNo; 
     [keyboardText setDelegate:self]; 
     [self.view addSubview:keyboardText]; 

    } 
    return self; 
} 

以下,但我的应用程序在
“tapDetectingWindow.viewToObserve = webView的”
与崩溃报告* - [一个UIWindow setViewToObserve:]:无法识别的选择发送到实例0x4a26b90

任何一个能帮助我......

回答

4

在p在上面的代码中并不存在这样的问题。它由于iOS 5.0中的新属性介绍而在UIApplicationDelegate协议下具有名称窗口。

在appdelegate文件中更改窗口名称。

在.h文件中

@class WebViewController; 
@class TapDetectingWindow; 
@interface test_touchAppDelegate : NSObject <UIApplicationDelegate> 
{ 
    TapDetectingWindow *myWindow; 
    WebViewController *viewController; 
} 
@property (retain, nonatomic) TapDetectingWindow *myWindow; 
@property (retain, nonatomic) WebViewController *viewController; 
@end 

在.m文件

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    self.myWindow = [[TapDetectingWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    self.viewController = [[WebViewController alloc] init]; 
    self.myWindow.rootViewController = self.viewController; 
    [self.myWindow makeKeyAndVisible]; 
    return YES; 
} 

希望这会有所帮助。

+0

我改变了上面的代码,它在同一点崩溃,我可以知道什么是WebViewController在这里,IAM只使用一个视图控制器和子视图的WebView。 – Valli

+0

是否有任何替代方法来识别webview上的手势? – Valli

0

Apurv告诉你的是正确的,像魅力一样工作。我看到你要求另一种方法来识别webview上的手势(我假设它意味着点击UIWebView。)。答案是肯定的,你必须使用“UIWebView委托方法”。

#pragma mark - 
#pragma mark uiwebview delegate 

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 
{ 
    if(navigationType==UIWebViewNavigationTypeLinkClicked){ 
     NSURL *URL = [request URL]; 
     if(URL){ 
      [[UIApplication sharedApplication] openURL:URL]; 
      return NO; 
     } 
    } 
    return YES; 
} 

上面的代码,当你点击UIWebView,它会打开的UIWebView您单击的链接。祝你好运。