触摸内部时,UIPopoverController会解散

时间:2012-10-28 21:09:44

标签: ios uipopovercontroller

我有一个简单的UIPopoverController,可以加载Youtube页面。所有工作都很好,除了当触摸里面它的界限时,popover似乎随机解散。

这似乎并不一致(关于哪些触摸导致解雇,虽然它永远不是第一个),还有其他人看到过这种行为吗?

我在下面使用的代码,正在加载的URL仅用于测试。

-(void)showWebPopover
{
    UIWebView * webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, WEBVIEW_POPOVER_WIDTH, WEBVIEW_POPOVER_HEIGHT)];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://en.wikipedia.org/wiki/M"]]];
webView.backgroundColor = [UIColor blackColor];
//webView.scrollView.scrollEnabled = NO;

UIViewController *popOver = [[UIViewController alloc] init];
popOver.view.frame = CGRectMake(0, 0, WEBVIEW_POPOVER_WIDTH, WEBVIEW_POPOVER_HEIGHT);
popOver.view.backgroundColor = [UIColor blackColor];
popOver.contentSizeForViewInPopover = CGSizeMake(WEBVIEW_POPOVER_WIDTH, WEBVIEW_POPOVER_HEIGHT);
[popOver.view addSubview:webView];

self.webViewPopoverController = [[UIPopoverController alloc] initWithContentViewController:popOver];

CGRect rect = CGRectMake(self.view.frame.size.width/2, self.view.frame.size.width/2, 1, 1);
[self.webViewPopoverController presentPopoverFromRect:rect inView:self.view permittedArrowDirections:0 animated:YES];
}

1 个答案:

答案 0 :(得分:0)

可能为时已晚,但我刚刚遇到这个并通过将UITapGestureRecognizer附加到视图控制器来解决它。 (不是PopoverController,而是PopoverController使用的控制器。)

    func present (title:String, body:String, footer:String, sourceRect:CGRect) {
    if let fPopup = self.storyBoard.instantiateViewControllerWithIdentifier("GuideViewController") as? GuideViewController {
        popoverController = UIPopoverController(contentViewController: fPopup)
        fPopup.titleLabel.text = title
        fPopup.bodyTextField.text = body
        fPopup.footerLabel.text = footer

        //these 4 lines to set it up
        var touchRecognizer = UITapGestureRecognizer( target: self, action: Selector("handleTap:"))
        touchRecognizer.numberOfTapsRequired = 1
        touchRecognizer.numberOfTouchesRequired = 1
        fPopup.view.addGestureRecognizer(touchRecognizer)


        popoverController?.presentPopoverFromRect(sourceRect, inView: parentView, permittedArrowDirections: UIPopoverArrowDirection.Any, animated: true)
    }
}

func handleTap(sender: UITapGestureRecognizer) {
    if sender.state == .Ended {
        popoverController?.dismissPopoverAnimated(true)
    }
}