0

我正在尝试使用扩展 UITextField 的自定义控件,以便在激活文本字段时隐藏选择插入符号。The text field has a picker as its inputview, so I don't want the blinking cursor in the text field when the picker is active. 我最终会在自定义控件中添加类似披露图标的东西,但现在我只是想隐藏光标。我在这里得到了这个解决方案。

但是,我的应用程序抛出了一个异常:Selector 从objective-c 在已被 GC'ed 的 TestCustomControl.PickerTextField (0xC8BC970) 类型的托管对象上调用

如果我将自定义字段改回 UITextField,我不会收到错误消息。

这是我的自定义类:

using System.Drawing;
using MonoTouch.Foundation;
using MonoTouch.UIKit;

namespace TestCustomControl
{
    [Register("PickerTextField")]
    public class PickerTextField : UITextField
    {
        public override RectangleF GetCaretRectForPosition (UITextPosition position)
        {
            return RectangleF.Empty;
        }
    }
}

我已经将我的 Outlet 设置为 UITextField 和 PickerTextField,并尝试在我的 PC 上从 VS 2012 和 Mac 上的 Xamarin Studio 4.0.8(构建 2)运行它。在所有情况下,如果在 XCode 中我将字段设置为 UITextField 它工作正常(但我得到一个闪烁的光标),如果我将它设置为 PickerTextField 我得到GC异常。

如果有帮助,我可以将整个项目的代码发布到某个地方(它只是一个 ViewController 和 View、自定义控件类、我的选择器的模型和 XIB 文件,以及为新项目创建的其他文件)。

4

1 回答 1

1

您缺少IntPtr构造函数。如果实例是从 XIB 创建的,则这是必需的。添加默认 c'tor 也是一个好主意:

   [Register("PickerTextField")]
        public class PickerTextField : UITextField
        {
            public PickerTextField(IntPtr handle) : base(handle)
            {}

public PickerTextField() : base()
            {}

            public override RectangleF GetCaretRectForPosition (UITextPosition position)
            {
                return RectangleF.Empty;
            }
        }

这很有趣,因为当我开始使用 MonoTouch 时,我实际上问过同样的问题:如何在带有 Monotouch 的 XIB 编辑器中使用自定义 UIView 子类?

于 2013-05-23T19:26:38.177 回答