【问题标题】:TapDetectingWindow on WebView is not Working ProperlyWebView 上的 TapDetectingWindow 无法正常工作
【发布时间】:2012-01-25 07:31:53
【问题描述】:

在我的应用程序中,我使用 TapDetectingWindow 来检测 UIWebView 上的触摸(与 http://mithin.in/2009/08/26/detecting-taps-and-events-on-uiwebview-the-right-way 中的详细信息完全相同)。 我按照建议在一个视图控制器中使用了它,并在头文件中引用了它,并在实现文件中使用了以下内容,例如

- (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:]: unrecognized selector sent to instance 0x4a26b90

谁能帮帮我...

【问题讨论】:

    标签: iphone uiwebview


    【解决方案1】:

    问题不在上面的代码中。这是由于 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;
    }
    

    希望这会有所帮助。

    【讨论】:

    • 我更改了上面的代码,它在同一点崩溃,我能知道这里的 WebViewController 是什么,我只使用一个视图控制器并查看 WebView。
    • 是否有其他方法可以识别 web 视图上的手势?
    【解决方案2】:

    Apurv 告诉您的内容是正确的,而且效果很好。我看到您要求使用替代方法来识别 web 视图上的手势(我假设这意味着单击 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的链接。祝你好运。

    【讨论】:

      猜你喜欢
      • 2017-03-16
      • 2018-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多