2014-10-01 77 views
-2

我想先說我對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 

任何幫助,將不勝感激。

回答

0

NSDictionary將是一個很好的方法。您可以爲每個文本字段值設置唯一的密鑰。

0
-(void)textFieldShouldReturn:(UITextField*)textField; 

應該是:

- (BOOL)textFieldShouldReturn:(UITextField*)textField; 

另外,不要忘了返回TRUE或FALSE(或YES或NO)。

要做到這一點,請在您的類中實現UITextFieldDelegate協議。

把下面的代碼行中的.m文件,在@implementation以上:

nameField.delegate = self; 
ageField.delegate = self; 
sexField.delegate = self; 

這樣的textFieldShouldReturn:

@interface CustomLayout() <UITextFieldDelegate> 
@end 

然後,用設置委託在UITextFields:方法將在用戶按下'輸入'時調用

0

幾件事:

  1. 鑑於您的委託方法,我假設您已經爲您的三個UITextField控件實際指定了delegate作爲您實現這些各種委託方法的對象。

  2. 如果你想控制的返回鍵的行爲,實現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中,我會確保前兩個控件的「返回鍵」設置爲「下一步」,最後一個設置爲「開始」或「完成」。

  3. 您節省日常只會填充對象的三個控件的內容:

    - (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 
    

    如果你寧願使用NSDictionaryNSArray,這也很好(但請確保您檢查的文本字段不是nil,雖然),但我個人更喜歡上面定義好的模型對象。

  4. 作爲改進,您可能希望確保只對年齡輸入數值(如果這就是你想要保存的年齡):

    - (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; 
    } 
    
+0

謝謝大家對你的答案!不幸的是,由於缺乏聲譽,我無法向您的答案投票! @Rob,但一個問題。爲了調用textFieldShouldReturn,語法變爲[nameField textFieldShouldReturn:YES]; ? – tk1863 2014-10-01 15:01:39

+0

不,你不叫它。當用戶在文本字段中輸入返回鍵時,操作系統會爲您調用它(假設您已經爲文本字段設置了代理屬性,無論是在IB中還是以編程方式)。 – Rob 2014-10-01 15:07:16

+0

@ tk1863注意,該方法需要一個'BOOL'返回,而不是像原始問題代碼示例中的'void'。 – Rob 2014-10-02 01:38:15

相關問題