我想先說我對iOS編程相當陌生,所以請原諒我的無知。以編程方式將數據從UITextFields存儲到NSArray中iOS
我在我的CustomLayout中有三個UITextFields。我要求用戶填寫他們的姓名,年齡和性別。如果可能的話,我想要兩件事。首先,我希望,只要用戶點擊鍵盤上的返回按鈕,輸入字符串將存儲在NSArray中。另外,第二個目標是當用戶點擊相同的按鈕時遍歷UITextFields。
// 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];
[email protected]"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
任何幫助,將不勝感激。
謝謝大家對你的答案!不幸的是,由於缺乏聲譽,我無法向您的答案投票! @Rob,但一個問題。爲了調用textFieldShouldReturn,語法變爲[nameField textFieldShouldReturn:YES]; ? – tk1863 2014-10-01 15:01:39
不,你不叫它。當用戶在文本字段中輸入返回鍵時,操作系統會爲您調用它(假設您已經爲文本字段設置了代理屬性,無論是在IB中還是以編程方式)。 – Rob 2014-10-01 15:07:16
@ tk1863注意,該方法需要一個'BOOL'返回,而不是像原始問題代碼示例中的'void'。 – Rob 2014-10-02 01:38:15