我有4个TextFields
,我想遍历它们以检查它们是否具有值。 TextFields
是根据我的GetCell
的TableViewController
方法创建的。
public UITextField TextField;
public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
{
if (elements [indexPath.Row].Type == "textField") {
EditField element = elements [indexPath.Row] as EditField;
NSString FieldID = new NSString ("EditField");
UITableViewCell cell = tableView.DequeueReusableCell (FieldID);
cell.SelectionStyle = UITableViewCellSelectionStyle.None;
var setTextField = cell.ViewWithTag (99) as UITextField;
if (setTextField == null) {
TextField = new UITextField ();
TextField.Placeholder = element.Placeholder;
TextField.Tag = 99;
TextField.SecureTextEntry = element.Secure;
cell.AddSubview (TextField);
EditFieldProperties ();
}
cell.TextLabel.Text = element.Label;
cell.DetailTextLabel.Hidden = true;
return cell;
}
}
如何循环所有
TextFields
以获取所有值?我想我需要将它们存储在array
或dictionary
中,但我不知道如何存储。我所能得到的就是带有以下代码的最后一个
TextField
的值:Console.WriteLine(TextField.Text);
最佳答案
我建议创建一个文本字段列表,因此在代码中的某些位置定义该列表,并在构造函数中的init位置
public List<UITextField> YourTextFields = new List<UITextField>();
public YouTableViewSourceConstructor()
{
foreach(var elementItem in elements.Where(e => e.Type == "textField").ToList())
{
YourTextFields.Add(new UITextField(){Tag = 99});
}
}
然后在GetCell方法中
public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
{
//some your code
if(cell.ViewWithTag (99) != null)
{
cell.RemoveSubview(cell.ViewWithTag (99));
}
var textField = YourTextFields [elements.Where(e => e.Type == "textField").ToList().IndexOf(elements [indexPath.Row])];
cell.AddSubview (textField);
//some your code
}
因此,在YourTextFields中,您将拥有所有4个文本字段,并且可以轻松访问它们
关于c# - 获取MonoTouch中多个TextField的值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22833581/