我首先要说的是我对iOS编程很新,所以请原谅我的无知。
我的CustomLayout中有三个UITextField。我要求用户填写他们的姓名,年龄和性别。如果可能的话,我想要两件事。首先,我想,一旦用户从键盘点击返回按钮,输入字符串将存储在NSArray中。此外,次要目标是在用户点击相同按钮时迭代UITextField。
// CustomLayout.h
@interface CustomLayout : UIView {
UITextField *nameField;
UITextField *ageField;
UITextField *sexField;
UILabel *nameLabel;
UILabel *ageLabel;
UILabel *sexLabel;
UIButton *startButton;
}
@property (nonatomic, strong) UITextField *nameField;
@property (nonatomic, strong) UITextField *ageField;
@property (nonatomic, strong) UITextField *sexField;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *ageLabel;
@property (nonatomic, strong) UILabel *sexLabel;
@property (nonatomic, strong) UIButton *startButton;
-(void)textFieldShouldReturn:(UITextField*)textField;
@end
在实施档案
中//CustomLayout.m
@implementation CustomLayout
@synthesize nameField, ageField, sexField;
@synthesize nameLabel, ageLabel, sexLabel;
@synthesize startButton;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setBackgroundColor:[tkStyle viewBackgroundColor]];
NSString *startButtonLabel = @"Start Experiment";
//alocate and position views
CGRect viewRect;//placeholder rect, reused for each view
//nameLabel and nameField
nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(120, 95, 150, 40)];
nameLabel.textColor = [UIColor colorWithRed:106/256.0 green:180/256.0 blue:150/256.0 alpha:1.0];
nameLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:25];
nameLabel.backgroundColor=[tkStyle viewBackgroundColor];
nameLabel.text=@"Enter Name:";
nameField = [[UITextField alloc] initWithFrame:CGRectMake(280, 90, 200, 40)];
nameField.textColor = [UIColor colorWithRed:0/256.0 green:84/256.0 blue:129/256.0 alpha:1.0];
nameField.font = [UIFont fontWithName:@"Helvetica-Bold" size:25];
nameField.borderStyle = UITextBorderStyleRoundedRect;
nameField.backgroundColor=[tkStyle viewBackgroundColor];
textFieldShouldReturn:nameField.text;
// same for ageField and sexField
//startButton
viewRect = CGRectMake(250, sexField.frame.origin.y+75, 200, 40);
startButton = [[UIButton alloc] initWithFrame:viewRect];
[startButton setTitle:startButtonLabel forState:UIControlStateNormal];
[startButton setTitleEdgeInsets:UIEdgeInsetsMake(4, 0, 0, 0)];
[startButton setButtonIsActive:true];
//[startButton setOSCAddress:OSCStopPressedString];
[startButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchDown];
//and so on adjust your view size according to your needs
[self addSubview:nameField];
[self addSubview:ageField];
[self addSubview:sexField];
[self addSubview:nameLabel];
[self addSubview:ageLabel];
[self addSubview:sexLabel];
[self addSubview:startButton];
}
return self;
}
// that should allow for users to hit 'return' button to move through textfields
-(void)textFieldShouldReturn:(UITextField*)textField;
{
//[(NSArray *) userInfoArray addObject:textField.text];
}
// that should change Views as soon as the user presses 'Start Experiment'
-(void)buttonAction:(id)sender
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"startTest" object:self];
}
@end
任何帮助都将不胜感激。
答案 0 :(得分:0)
NSDictionary将是一个很好的方法。您可以为每个文本字段值使用唯一键。
答案 1 :(得分:0)
-(void)textFieldShouldReturn:(UITextField*)textField;
应该是:
- (BOOL)textFieldShouldReturn:(UITextField*)textField;
另外,不要忘记返回TRUE或FALSE(或YES或NO)。
要使其工作,请在您的类中实现UITextFieldDelegate协议。
将以下代码行放在.i文件中,@ implementation:
上方@interface CustomLayout () <UITextFieldDelegate>
@end
然后,使用以下命令在UITextFields中设置委托:
nameField.delegate = self;
ageField.delegate = self;
sexField.delegate = self;
这样当用户按下&#39;输入&#39;
时,将调用textFieldShouldReturn:方法答案 2 :(得分:0)
有几件事:
根据您的委托方法,我假设您实际上已将三个delegate
控件的UITextField
指定为您已在其中实施这些各种委托的对象方法
如果要控制返回键的行为,请实现textFieldShouldReturn
方法:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if (textField == self.nameField) {
[self.ageField becomeFirstResponder];
} else if (textField == self.ageField) {
[self.sexField becomeFirstResponder];
} else if (textField == self.sexField) {
[textField resignFirstResponder];
[self saveResults];
}
return NO;
}
我通常倾向于做类似上面的事情,按下返回将我带到下一个字段,按下最后一个字段的返回将解除键盘并尝试保存结果。
顺便说一下,在IB中,我确保&#34;返回键&#34;前两个控件的设置为&#34; Next&#34;,最后一个控件的设置为&#34; Go&#34;或者&#34;完成&#34;。您的保存例程只会使用三个控件的内容填充对象:
- (void)saveResults
{
Person *person = [[Person alloc] init];
person.name = self.nameField.text;
if (self.ageField.text) {
person.age = @([self.ageField.text integerValue]);
}
person.sex = self.sexField.text;
// now do whatever you want with this object
}
这显然假设您有一个Person
类:
@interface Person : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, strong) NSNumber *age;
@property (nonatomic, copy) NSString *sex;
@end
@implementation Person
@end
如果您更倾向于使用NSDictionary
或NSArray
,那也很好(请确保您检查的文字字段不是nil
)虽然我个人更喜欢上面定义良好的模型对象。
作为一项改进,您可能希望确保只输入年龄的数值(如果您想要存储年龄的话):
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (textField == self.ageField) {
NSCharacterSet *invalid = [[NSCharacterSet characterSetWithCharactersInString:@"1234567890"] invertedSet];
NSRange range = [string rangeOfCharacterFromSet:invalid];
return range.location == NSNotFound; // if non-numeric character not found, return true
}
return YES;
}