gpt4 book ai didi

ios - 仅在 UIAlertView 之后选择焦点上的 UITextView 中的所有文本

转载 作者:行者123 更新时间:2023-11-29 13:24:37 25 4
gpt4 key购买 nike

我有一个包含一些 UITextView 控件的 tableView。当用户点击其中之一时,应选择其中的文本,以便任何键盘输入立即替换原始内容。

我无法使用此代码获取选择的 UITextView 中的文本:

txtQuantity.SelectAll (new NSObject(NSObjectFlag.Empty));

因为此代码仅显示菜单“选择 | 全选”,而没有实际选择文本。

有人让这个工作了吗?

编辑:下面的代码选择 txtQuantity 控件内的文本,但前提是首先显示 UIAlert!这是为什么?

public override void ViewDidLoad ()
{
base.ViewDidLoad ();

txtQuantity.TouchDown += txtQuantityHandleTouchDown;

txtQuantity.EditingDidBegin += delegate {

txtQuantity.ExclusiveTouch=true;
UIAlertView uv = new UIAlertView("","OK",null,"OK",null);
uv.Show ();
};

}

void txtQuantityHandleTouchDown (object sender, EventArgs e)
{
txtQuantity.SelectAll (this);
txtQuantity.Selected = true;

}

如果 txtQuality.EditingBegin 委托(delegate)中的所有代码都被注释掉,则不会触发 HandleTouchDown 事件。

最佳答案

我不确定这是否是您想要的,但我整理了一个快速示例。

我遇到的问题是在 EditingDidBegin 中调用 SelectAll。我必须调用 BeginInvokeOnMainThread 才能让选择工作。我不确定主线程上没有发生事件是否有问题,或者您只需要在主线程上进行异步调用。

using System;
using System.Collections.Generic;
using System.Linq;

using MonoTouch.Foundation;
using MonoTouch.UIKit;

namespace SelectText
{
[Register ("AppDelegate")]
public partial class AppDelegate : UIApplicationDelegate
{
// class-level declarations
UIWindow window;

public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
// create a new window instance based on the screen size
window = new UIWindow (UIScreen.MainScreen.Bounds);

window.RootViewController = new MyTableViewController ();

// make the window visible
window.MakeKeyAndVisible ();

return true;
}
}

public class MyTableViewController : UITableViewController
{
public override void LoadView ()
{
base.LoadView ();
this.TableView.DataSource = new TableViewDataSource ();
}
private class TableViewDataSource : UITableViewDataSource
{
private class EditCell : UITableViewCell
{
UITextField _field;

public EditCell () : base (UITableViewCellStyle.Default, "mycell")
{
_field = new UITextField (this.Bounds);
_field.AutoresizingMask = UIViewAutoresizing.All;
_field.BackgroundColor = UIColor.Clear;
_field.ShouldReturn = delegate {
_field.ResignFirstResponder ();

return true;
};

_field.EditingDidBegin += delegate {
this.BeginInvokeOnMainThread ( delegate {
_field.SelectAll (this);
});
};

_field.Text = "Some Text";
this.Add (_field);
}

public override void LayoutSubviews ()
{
base.LayoutSubviews ();
_field.Frame = this.Bounds;
}


}

#region implemented abstract members of UITableViewDataSource
public override int RowsInSection (UITableView tableView, int section)
{
return 2;
}

public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
{
UITableViewCell cell = tableView.DequeueReusableCell ("mycell");

if (cell == null)
{
cell = new EditCell ();
}

cell.SelectionStyle = UITableViewCellSelectionStyle.None;

return cell;
}
#endregion
}
}
}

关于ios - 仅在 UIAlertView 之后选择焦点上的 UITextView 中的所有文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13461009/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com