Ios 如何使用带有标记的objective c从textfield访问文本值?
事实上,我对目标c还不熟悉。我的问题是如何从带有标记的textfield中获取文本。其中文本字段位于嵌套视图中。因此,我无法从嵌套视图中获取文本字段的值。如何使用objective c从stackview中提取文本 如图所示,我只从最后一个文本字段中获取文本,而不是从上面两个字段。我是动态创建的文本字段,如何获取每个文本字段的文本Ios 如何使用带有标记的objective c从textfield访问文本值?,ios,objective-c,uistackview,Ios,Objective C,Uistackview,事实上,我对目标c还不熟悉。我的问题是如何从带有标记的textfield中获取文本。其中文本字段位于嵌套视图中。因此,我无法从嵌套视图中获取文本字段的值。如何使用objective c从stackview中提取文本 如图所示,我只从最后一个文本字段中获取文本,而不是从上面两个字段。我是动态创建的文本字段,如何获取每个文本字段的文本 else if ([key isEqualToString: textbox]){ NSDictionary *widthAndHieght
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);
}
}
}
-(void)sendBridge:(UITextField*)textField{
对于(int i=0;i<[ArrayFTAGValue count];i++){
NSLog(@“值:%@,[ArrayFTAGValue对象索引:i]);
如果(textField==myTextField){
myTextField.textColor=[UIColor redColor];
NSLog(@“text:%@”,myTextField.text);
}
}
}
请使用以下命令:-
UITextField *txt = (UITextField*)[self.view viewWithTag:0];
NSString *strValue = txt.text;
以下是一个例子:
在viewcontroller.h中
@interface UIViewController : UIViewController<UITextFieldDelegate> {
UITextField * listTitle;
}
希望它能有所帮助,您可以在 IBOutletCollection并这样引用它们。通过这种方式,您可以遍历包含文本字段的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
如果使用UIStackview,则可以执行以下操作:
不管怎样,回答得很好,乔治
但是我没有使用self.view,我使用的是堆栈视图。根据,我希望从嵌套的stackview中获取值。
#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
for (UITextField *txtField in [stackview subviews]) {
if (txtField.tag == 1) {
//...
} else {
//...
}
}