Monotouch:了解委托机制模式

发布于 2024-10-20 01:20:11 字数 1040 浏览 7 评论 0原文

我没有完全理解单点触控中的委托机制。谁能帮助我理解这个概念?

问题很简单。我将尝试将我在 Objective C 中所做的事情映射到 Monotouch 中。

例如,假设我在 Objective C 中的 MyController 中创建了一个 UIPopoverController。在 Objective C 中,代码如下:

@interface MyController : UIViewController <UIPopoverControllerDelegate> {
    // ...
}
// ...
@end

MyController 内,我可以实例化一个 UIPopoverController,如下所示:

UIPopoverController *popover = // ...
popover.delegate = self;

最后是委托中使用的方法。

那么,Monotouch 怎么样?

通过这段代码,我可以实例化 MyController 类中的 UIPopoverController,该类扩展了特定 TouchUpInside 事件处理程序中的 UIViewController

popover = new UIPopoverController(new CustomController());
popover.PopoverContentSize = new SizeF(200f, 200f);
popover.PresentFromRect(button.Frame, containerForButtonView, UIPopoverArrowDirection.Left, true);

PS 一个重要的方面是放置 popover作为成员类引用,而不是作为处理程序内的局部变量,因为 monotouch GC 运行良好!

先感谢您。

I didin't completely understand the delegate mechanism in monotouch. Can anyone help me to understand this concept?

The question is simple. I'll try to map what I've done in Objective C in Monotouch.

For example, suppose I've creating a UIPopoverController in Objective C inside MyController. In Objective C the code is the following:

@interface MyController : UIViewController <UIPopoverControllerDelegate> {
    // ...
}
// ...
@end

Inside MyController I can istantiate a UIPopoverController like the following:

UIPopoverController *popover = // ...
popover.delegate = self;

and finally methods used in the delegate.

So, what about Monotouch?

Through this code I can istantiate the UIPopoverController inside MyController class that extends UIViewController inside a specific TouchUpInside event handler:

popover = new UIPopoverController(new CustomController());
popover.PopoverContentSize = new SizeF(200f, 200f);
popover.PresentFromRect(button.Frame, containerForButtonView, UIPopoverArrowDirection.Left, true);

P.S. An important aspect is to put popover reference as a member class and not as a local variable inside the handler because the monotouch GC works well!!!

Thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

失去的东西太少 2024-10-27 01:20:11

这实际上与 C# 的关系比 MonoTouch 本身的关系更大。在 MonoTouch 中,UIPopoverControllerDelegate 是一个类,而 C# 不允许多重继承,因此无法与 Obj-C 一对一翻译代码。不过,有一种更简单的方法(下面的代码可以编译,但显然不起作用):

public class MyController: UIViewController {
        public void mymethod(){
            var popover = new UIPopoverController();
            popover.DidDismiss += HandlePopoverDidDismiss;
            popover.PopoverContentSize = new SizeF(200f, 200f);
            popover.PresentFromRect(button.Frame, containerForButtonView, UIPopoverArrowDirection.Left, true);
        }

        void HandlePopoverDidDismiss (object sender, EventArgs e)
        {
            Console.WriteLine("Working!");
        }
    }
}

如您所见,您可以将事件处理程序添加到弹出窗口中的 DidDismiss 事件中,这将执行以下操作你想要的。一般来说,Obj-C 中所有控件中由委托处理的所有事件都可以这样使用。您还可以内联编写该方法,如下所示:

popover.DidDismiss += delegate {
    //dosomething
};

希望这就是您正在寻找的。

This really has more to do with C# than MonoTouch itself. In MonoTouch, UIPopoverControllerDelegate is a class, and C# doesn't allow multiple inheritance, so you can't translate code one to one with Obj-C. There's an easier way out though (code below compiles, but obviously doesn't work):

public class MyController: UIViewController {
        public void mymethod(){
            var popover = new UIPopoverController();
            popover.DidDismiss += HandlePopoverDidDismiss;
            popover.PopoverContentSize = new SizeF(200f, 200f);
            popover.PresentFromRect(button.Frame, containerForButtonView, UIPopoverArrowDirection.Left, true);
        }

        void HandlePopoverDidDismiss (object sender, EventArgs e)
        {
            Console.WriteLine("Working!");
        }
    }
}

As you can see, you can add an event handler to to the DidDismiss event in the popover, which will do what you want. In general, all events that in Obj-C are handled by the delegate in all controls can be used this way. You can also write the method inline, like this:

popover.DidDismiss += delegate {
    //dosomething
};

Hope this is what you're looking for.

生寂 2024-10-27 01:20:11

这并不能回答您针对您的 UIPopovercontroller 的问题,我想您会找到此链接Monotouch 文档中的内容非常有用。 它解释了 Objective-C 委托和 C# 委托之间与 Monotouch 相关的差异。关于您的具体问题,我没有时间快速编写一个测试用例来完全理解它,但我想我会发布该链接,以便您同时可以阅读一些内容!

This doesn't answer your question specific to your UIPopovercontroller I think you will find this link from the Monotouch Docs useful. It explains the differences between Objective-C delegates and C# delegates with relation to Monotouch. With regards to your specific problem, I havent got time to whip up a quick test case to understand it fully but figured I'd post that link so you've got something to read in the mean time!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文