5

我已经通过 stackoverflow 搜索了一个类似的问题,并找到了对其他人有用但没有一个对我有用的推荐解决方案,所以我开始怀疑这是我没有正确完成的事情。

这很简单。我想要的是,当用户点击 uitextfield 时,uitextfield 中的整个文本都会被选中。然后,用户可以继续将其全部删除,点击一次并从该点开始追加或开始输入以覆盖所有内容。

我有一个来自 uitextfield 的操作,这是代码段。

- (IBAction)didBeginEditDescription:(id)sender
{
    NSLog(@"Description began edit.");

    [self.txtfield selectall:self];
}

我知道该方法被调用(由 NSLog 可见)。但是,什么也没有发生,光标仍然在文本的最后一个位置。我已经有了 UITextFieldDelegate,所以不确定我还应该看什么?

仅供参考,这是在 Xcode 5.0 上完成的,并试图为 iOS 7 开发。

有什么明显的我失踪了吗?

4

4 回答 4

9

我认为您想突出显示 selected 的所有文本UITextField。在这种情况下,首先您应该确定哪个UITextField调用了该方法 ( -didBeginEditDescription),然后您可以调用-selectAll该特定的UITextField.

示例代码:

- (IBAction)didBeginEditDescription:(id)sender
{
    NSLog(@"Description began edit.");
    UITextField *txtFld = ((UITextField *)sender);
    [txtFld selectAll:self];
}

更新 :

-selectAll应该管用。我已经实施了,它对我来说效果很好。请注意,您写-selectall的是-selectAll. 你也可以这样尝试:

[txtFld setSelectedTextRange:[txtFld textRangeFromPosition:txtFld.beginningOfDocument toPosition:txtFld.endOfDocument]]; 
于 2013-10-02T10:32:00.693 回答
4

Xamarin iOS:

nativeTextField.EditingDidBegin += (object sender, EventArgs eIos) =>
                {
                   nativeTextField.PerformSelector(new Selector("selectAll"), null, 0.0f);
                };

适用于 iOS 的 Xamarin.Forms 自定义渲染器:

using System;
using CoreText;
using ObjCRuntime;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;


[assembly: ExportRenderer(typeof(Entry), typeof(MyEntryRenderer))]
namespace Hello.iOS
{    
    public class MyEntryRenderer : EntryRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
        {
            base.OnElementChanged(e);    
            if (Control != null)
            {
                var nativeTextField = (UITextField)Control;    
                nativeTextField.EditingDidBegin += (object sender, EventArgs eIos) =>
                {
                   nativeTextField.PerformSelector(new Selector("selectAll"), null, 0.0f);
                };                           
            }
        }

    }

适用于 Android 的 Xamarin.Forms 自定义渲染器:

  protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
    {
        base.OnElementChanged(e);

        if (Control != null)
        {


            EditText editText = null;

            for (int i = 0; i < ChildCount; ++i)
            {
                Android.Views.View view = (Android.Views.View)GetChildAt(i);
                if (view is EditText) editText = (EditText)view;
            }

            editText?.SetSelectAllOnFocus(true);            

        }
    }
于 2017-09-29T09:34:26.363 回答
1

对于 Xamarin.Forms,使用此方法创建自定义渲染器:

protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
    base.OnElementChanged(e);
    if (e.NewElement == null) return;
    Control.EditingDidBegin += (sender, e) => Control.PerformSelector(new ObjCRuntime.Selector("selectAll"), null, 0.0f);
}
于 2018-02-02T16:26:00.500 回答
1

我发现有必要打电话selectAll:selfTouchDown:不是editDidBegin:

Touch Down- (IBAction)didBeginEditDescription:(id)senderInterfaceBuilder 中附加。

于 2016-02-18T13:58:32.437 回答