如何使用带有标签的目标 c 从文本字段访问文本值?
Posted
技术标签:
【中文标题】如何使用带有标签的目标 c 从文本字段访问文本值?【英文标题】:How to access text value from textfield using objective c with tag? 【发布时间】:2017-05-22 14:31:52 【问题描述】:Output image实际上我是目标 c 的新手。我的问题是如何从带有标签的文本字段中获取文本。文本字段位于嵌套的 uistackview 内。所以我无法从嵌套的 uistackview 中获取文本字段的值。如何使用objective c从stackview中获取文本?
如图所示,我仅从最后一个文本字段而不是从以上两个文本字段获取文本。我是动态创建的文本字段,我应该如何获取每个文本字段的文本?
else if ([key isEqualToString: textbox])
NSDictionary *widthAndHieght = [rowNames objectForKey:key];
//NSLog(@"Hieght: %@",[widthAndHieght objectForKey:@"height"]);
//NSLog(@"Width: %@",[widthAndHieght objectForKey:@"width"]);
int height = [[widthAndHieght objectForKey:@"height"]intValue];
int width = [[widthAndHieght objectForKey:@"width"]intValue];
NSString *intTag = [[widthAndHieght objectForKey:@"tag"] objectForKey:@"tagInt"];
myTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, width, height)];
myTextField.borderStyle = UITextBorderStyleRoundedRect;
myTextField.font = [UIFont systemFontOfSize:15];
//NSString *textFieldTag = [NSString stringWithFormat:@"%@", tagValue];
//NSString *textFieldTag = myTextField.text;
NSInteger b = [intTag integerValue];
myTextField.tag = b;
NSLog(@" Textfield tag:%ld",(long)myTextField.tag);
myTextField.keyboardType = UIKeyboardTypeDefault;
myTextField.returnKeyType = UIReturnKeyDone;
myTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
//[layout addSubview:myTextField];
[layout addArrangedSubview:myTextField];
NSNumber *tagNumber = [NSNumber numberWithInteger:b];
[arrayOfTagValue addObject:tagNumber];
NSLog(@" tag values :%@",arrayOfTagValue);
NSDictionary *bindings3 = NSDictionaryOfVariableBindings(layout, myTextField);
// Set the constraints for the label to be pinned equally with padding
[greyView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(30)-[myTextField]-(30)-|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:bindings3]];
[greyView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(30)-[myTextField]-(30)-|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:bindings3]];
下面是我需要获取每个文本值但无法实现的代码。怎么做?
-(void) sendBridge:(UITextField *)textField
for(int i = 0; i< [arrayOfTagValue count]; i++)
NSLog(@"Value :%@",[arrayOfTagValue objectAtIndex:i]);
if(textField == myTextField)
myTextField.textColor = [UIColor redColor];
NSLog(@" text :%@",myTextField.text);
【问题讨论】:
【参考方案1】:请使用这个:-
UITextField *txt = (UITextField*)[self.view viewWithTag:0];
NSString *strValue = txt.text;
【讨论】:
但我没有使用 self.view 我正在使用堆栈视图。【参考方案2】:这是一个例子:
在你的 viewcontroller.h 中
@interface UIViewController : UIViewController<UITextFieldDelegate>
UITextField * listTitle;
在您的 viewController.m 中:
listTitle = (UITextField*)[self.view viewWithTag:10];
listTitle.userInteractionEnabled=NO;
listTitle.delegate = self;
listTitle.textAlignment=NSTextAlignmentCenter;
listTitle.font =[UIFont fontWithName:@"Lato-Bold" size:14];
listTitle.textColor =[UIColor colorWithRed:45/255.0f green:45/255.0f blue:54/255.0f alpha:1];
listTitle.text=@"";
NOW you can use delegate methods:
-(void)textFieldDidEndEditing:(UITextField *)textField
if(textField == listTitle) //or textField.tag == 10
//here you know the user is using your textfield listTitle
-(void) textFieldDidBeginEditing:(UITextField *)textField
if(textField == listTitle)
//here you know the user is using your textfield listTitle
listTitle.textcolor = [UIColor redColor];
listTitle.text = @"Hello";
希望对你有帮助
【讨论】:
不,它不起作用。根据我希望从嵌套的 stackview 中获取值。【参考方案3】:您可以将 TextFields 添加到
中,而不是使用标签IBOutletCollection 并像那样引用它们。这样,您可以遍历包含 TextField 的 IBOutletCollection 数组并获取值。
这比标签安全得多,因为它不太容易出现用户错误。
示例:
#import "ViewController.h"
@interface ViewController ()
@property (strong, nonatomic) IBOutletCollection(UITextField) NSArray *TextFieldsCollection;
@end
@implementation ViewController
- (void)viewDidLoad
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
for (UITextField *textField in self.TextFieldsCollection)
NSLog(@"Placeholder: %@", textField.placeholder);
NSLog(@"Text: %@", textField.text);
@end
【讨论】:
【参考方案4】:如果您使用的是 UIStackview,您可以这样做:
for (UITextField *txtField in [stackview subviews])
if (txtField.tag == 1)
//...
else
//...
乔治回答得很好!
【讨论】:
以上是关于如何使用带有标签的目标 c 从文本字段访问文本值?的主要内容,如果未能解决你的问题,请参考以下文章
在 UITableview 中,键入的文本字段值在不同的单元格中重复,如何使用目标 c 纠正它
如何在表格视图单元格中添加 12 个文本字段并通过其标记值访问每个文本字段的文本?