Monotouch、UIPopover 和获取事件

Posted

技术标签:

【中文标题】Monotouch、UIPopover 和获取事件【英文标题】:Monotouch, UIPopover and getting the event 【发布时间】:2012-07-12 12:08:46 【问题描述】:

我是 Monotouch 的新手,但我在遥远的过去做过一些 Objective C。我正在尝试弄清楚我将如何执行以下操作:

    我有一个带有导航栏和按钮的 ViewController。 当我单击按钮时,我希望出现一个 UIPopOver,其中可能包含一个表格视图或按钮。

我要解决的是如何从放置在 UIPopOver 中的视图中获取委托/事件,以将其事件发送回调用视图。

在 Objective C 中,您可以像这样在视图的调用中设置委托:

 UIPopoverController *popover = 
        [[UIPopoverController alloc] initWithContentViewController:myPopOverViewcontrol]; 
    popover.delegate = self;

那么在 Monotouch 中,您是如何做到这一点的呢?如果像这样设置 UIPopOver:

UIPopoverController myPopOver = new UIPopoverController(new myPopOverViewControl());
myPopOver.PopoverContentSize = new SizeF(200f, 300f);
myPopOver.PresentFromRect (btnButton.Frame, v, UIPopoverArrowDirection.Up, true);

如何附加委托?

谢谢

迈克

【问题讨论】:

【参考方案1】:

您可以从三种样式中进行选择:(a) C# 事件样式,(b) 强类型/代码完成感知样式和 (c) 动态样式。

C#事件样式是这样的:

popover.DidDismiss += (sender, args) 
   Console.WriteLine ("Dismissed!");

如果使用上述方法,则只能在该对象上使用 C# 事件,不能在同一个对象中混搭。

强类型/代码完成感知风格是:

popover.Delegate = new MyUIPopoverControllerDelegate ();

[...]

class MyUIPopoverControllerDelegate : UIPopoverControllerDelegate 
     public override void DidDismiss (UIPopoverController popoverController);

动态样式不执行参数检查,也不会警告您错误,并且与您在 Objective-C 中所做的非常相似。您还需要手动找出从 Objective-C 选择器到方法的绑定:

popover.WeakDelegate = this;

[...]

[Export ("popoverControllerDidDismissPopover:")]
void dismissed (UIPopoverController controller)

    Console.WriteLine ("Dismissed");

【讨论】:

【参考方案2】:

在 MonoTouch 中,您可能会附加到 DidDismiss 事件:

var pop = new UIPopoverController();
pop.DidDismiss += (sender, args) => Console.WriteLine("Popover '0' was dismissed", (UIPopoverController)sender);
pop.PresentFromBarButtomItem(...);

【讨论】:

以上是关于Monotouch、UIPopover 和获取事件的主要内容,如果未能解决你的问题,请参考以下文章

MonoTouch 中的弱事件模式

如何获取 UIPopOver 选定的行文本

在 Monotouch 中的 UIPageViewController 中检测控件上的触摸事件

从 UITextField 单击事件 MonoTouch 在 UIDatePicker 中选择日期

MonoTouch 和 UIPopoverController

iOS 上 MonoTouch 中的多点触控