如何在uipopovercontroller后面的视图上禁用交互

时间:2013-10-21 21:41:44

标签: c# ios xamarin.ios ios7 xamarin

我正在使用UIPopoverController我有两个按钮,每个按钮在点击时都会显示一个弹出窗口。但是,我不希望弹出窗口同时显示 - 这意味着我不希望用户能够按下一个按钮,而弹出窗口显示时可以按下另一个按钮。似乎我已经尝试了所有方法 - 禁用按钮上的用户交互,隐藏弹出窗口后面的视图,使用pop的passthrough视图等等。它都不起作用!禁用用户交互似乎在大多数情况下有效,但随后停止禁止用户与按钮交互并导致应用程序崩溃......

popupView.PassthroughViews = new UIView[]{this.View.Superview, this.View, this.Gray}; //gray is another view that sits under the view that calls the popup
this.View.UserInteractionEnabled = false;
this.PositiveMeterBtn.UserInteractionEnabled = false; 
this.View.Hidden = true;

我的UIPopoverController是在班级宣布的,我甚至完成了这样的代码:

if(popupView != null)
    return;

我仍然会收到多个弹出窗口。我正在使用mono touch / xamarin - 这是xamarin或ios问题的错误吗?我是以正确的方式处理这件事吗?

3 个答案:

答案 0 :(得分:2)

我之前没有和Xamarin合作,但是在原生Objective-C中为我工作的是

[controller setModalInPopover:YES];

其中controller是popover中显示的视图控制器。

从UIViewController类引用:

@property(非原子,readwrite,getter = isModalInPopover)BOOL modalInPopover

此属性的默认值为NO。将其设置为YES会导致拥有的弹出控制器在显示时禁止此视图控制器外部的交互。

答案 1 :(得分:1)

您可以制作popover模式,但如果它不包含模态的内容,则不应阻止用户。

通常更好的选择是制作两个辅助方法并将它们放在例如app appate中。如果要显示另一个popover,则该方法会关注现有的popover。这样,您最多可以UIPopoverController,并且不必担心被解雇。

/// <summary>
/// Shows a popover.
/// </summary>
/// <param name='controllerToShow'>the controller to show in the popover</param>
/// <param name='showFromRect'>the rectangle to present the popover from. Not used if showFromItem is specified.</param>
/// <param name='showInView'>the view the popover is hosted in</param>
/// <param name='showFromItem'>the bar button item the popover gets presented from.</param>
/// <param name='popoverContentSize'>the content size of the popover</param>
/// <param name='animated'>If set to <c>true</c>, animated the popover</param>
/// <param name='arrowDirection'>the allowed arrow directions</param>
/// <param name='onDismiss'>callback if the popover gets dismissed. Careful that the object that owns the callback doesn't outlive the popover controller to prevent uncollectable memory.</param>
public static void ShowPopover(UIViewController controllerToShow, RectangleF showFromRect, UIView showInView, UIBarButtonItem showFromItem, SizeF popoverContentSize, bool animated = true, UIPopoverArrowDirection arrowDirection = UIPopoverArrowDirection.Any, EventHandler onDismiss = null)
{
    if(AppDelegateBase.popoverController != null)
    {
        AppDelegateBase.DismissPopover(false);
    }

    if(showFromItem == null && showFromRect.IsEmpty)
    {
        // Nothing to attach the popover to.
        return;
    }

    popoverController = new UIPopoverController(controllerToShow);
    if(!popoverContentSize.IsEmpty)
    {
        popoverController.SetPopoverContentSize(popoverContentSize, false);
    }

    if(onDismiss != null)
    {
        popoverController.DidDismiss += onDismiss;
    }

    // Send a notification that a popover will be presented.
    NSNotificationCenter.DefaultCenter.PostNotificationName("WillPresentPopover", popoverController);

    if(showFromItem != null)
    {
        popoverController.PresentFromBarButtonItem(showFromItem, arrowDirection, animated);
    }
    else 
    {
        popoverController.PresentFromRect(showFromRect, showInView, arrowDirection, animated );
    }
}

/// <summary>
/// Dismisses the popover presented using ShowPopover().
/// </summary>
/// <param name='animated'>If set to <c>true</c>, animates the dismissal</param>
public static void DismissPopover(bool animated = false)
{
    if(popoverController != null)
    {
        popoverController.Dismiss(animated);
    }
    AppDelegateBase.popoverController = null;
}
private static UIPopoverController popoverController;

答案 2 :(得分:0)

您可能尝试的一件事是使用方法

-(BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender

并在该方法中检查您的一个弹出视图控制器是否在屏幕上。

if (popupView.view.window) {
    return NO;
} else {
    return YES;
}