使UITextField成为第一个响应者

时间:2014-05-25 07:42:15

标签: ios xamarin.ios

我有一个UITextField的视图。当我点击" textfield"时,我调用一个调用键盘的方法,并使用一个按钮和新的UITextField添加一个工具栏。

我使用的方法是:

public static void SetKeyboardEditorWithCloseButton(UITextField txt, UIKeyboardType keyboardType)
    {
        var toolbar = new UIToolbar
        {
            BarStyle = UIBarStyle.Black,
            Translucent = true,

        };
        txt.KeyboardType = keyboardType;
        toolbar.SizeToFit();

        var text = new UITextView(new RectangleF(0, 0, 200, 32))
        {
            ContentInset = UIEdgeInsets.Zero,
            KeyboardType = keyboardType,
            Text = txt.Text,
            UserInteractionEnabled = true
        };
        var textField = new UIBarButtonItem(text);
        text.Layer.CornerRadius = 4f;
        text.BecomeFirstResponder();

        var doneButton = new UIBarButtonItem(StringUtils.GetString("Common.Done"), UIBarButtonItemStyle.Done,
                             (s, e) =>
            {
                text.ResignFirstResponder();
                txt.ResignFirstResponder();
                if (!string.IsNullOrEmpty(text.Text))
                    txt.Text = text.Text;
            });

        toolbar.UserInteractionEnabled = true;
        toolbar.SetItems(new UIBarButtonItem[]{ doneButton, textField }, true);

        txt.InputAccessoryView = toolbar;
    }

意图是当调用它时,工具栏上的UITextField应该成为第一个响应者。我是否正确地执行此操作,或者我是否应该执行其他操作以强制工具栏上的UITextField成为第一个响应者。从文本中撤出第一个响应者显然会关闭键盘。

0 个答案:

没有答案