从自定义控件中显示新视图

时间:2012-01-24 21:00:54

标签: xamarin.ios

我有一个继承自UIButton的自定义按钮。我正在处理TouchUpInside事件,并希望在当前视图的顶部显示视图。在Windows开发中是否存在像Dialog这样的东西?或者我应该以另一种方式做到这一点?

[MonoTouch.Foundation.Register("HRPicker")]
public class HRPicker : UIButton
{
    public HRPicker () : base()
    {
        SetUp();
    }

    public HRPicker(NSCoder coder) : base(coder)
    {
        SetUp();
    }

    public HRPicker(NSObjectFlag t) : base(t)
    {
        SetUp();
    }

    public HRPicker(IntPtr handle) : base(handle)
    {
        SetUp();
    }

    public HRPicker(RectangleF frame) : base(frame)
    {
        SetUp();
    }

    public void SetUp()
    {
        TouchUpInside += HandleTouchUpInside;
    }

    void HandleTouchUpInside (object sender, EventArgs e)
    {
        //I want to display a View here on top of the current one.
    }
}

谢谢,

2 个答案:

答案 0 :(得分:2)

是的,您有几个选择:

  • ModalViewController - 从任何UIViewController调用,并覆盖前台的ViewController。
  • UIPopoverController - 是一个本机控件,它接受UIViewController并具有用于演示和解雇的挂钩
  • WEPopoverController - 是UIPopoverController的重新实现,允许您自定义Popover容器的布局,大小和颜色。

ModalViewController:http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html

UIPopoverController:http://developer.apple.com/library/ios/#documentation/uikit/reference/UIPopoverController_class/Reference/Reference.html

WEPopoverController:https://github.com/mono/monotouch-bindings/tree/master/WEPopover

更新:无论您使用哪个选项,都必须从主线程调用Popover / Modal视图的演示文稿:

using(var pool = new NSAutoReleasePool()) {
    pool.BeginInvokeOnMainThread(()=>{

        // Run your awesome code on the 
        // main thread here, dawg.

    });
}

答案 1 :(得分:0)

Cocoa中对等的对话框是UIAlertView:http://developer.apple.com/library/ios/#documentation/uikit/reference/UIAlertView_Class/UIAlertView/UIAlertView.html

查看此问题以获取如何使用它的示例:Showing an alert with Cocoa

代码应该很容易转换为c#和MonoTouch。但这是一个简单的例子:http://monotouchexamples.com/#19