serge2487
serge2487

Reputation: 441

specifying textField using UITextField delegate

Hi i'm new to software for iphone/ipad apps. I am using the textField delegate method: "(BOOL)textFieldShouldReturn:(UITextField *)textField". I have gotten to the point where when the user enter a name and presses 'enter' and a label displays what the user just wrote. This is just two lines of code like so:

labelChange.text = textField.text;  //labelChange is my label in IB
return YES;

Since i have several textfields in IB, this code works for all of them. I'm not sure how to be specific with the code and have this label change for only ONE of my textFields. My .h files looks like this.

@interface FirstViewController : UIViewController <UITextFieldDelegate> {

IBOutlet UILabel *labelChange;
IBOutlet UITextField *userName;
IBOutlet UITextField *homeValue; 
IBOutlet UITextField *downPayment;
IBOutlet UITextField *textField;
}

@property (nonatomic,retain) UILabel *labelChange;
@property (nonatomic,retain) UITextField *userName;
@property (nonatomic,retain) UITextField *homeValue;
@property (nonatomic,retain) UITextField *downPayment;
@property (nonatomic,retain) UITextField *textField;

@end

I want my label to change only when user types in the text field labeled "userName".I'm not sure how to do this, am I missing something?In IB, I connected all my textfield delegates to 'Files Owner'. Any advice would be really helpful. thanks!

Upvotes: 0

Views: 2469

Answers (1)

Rog
Rog

Reputation: 18670

Assign each of your text fields a tag number i.e.

textField1.tag = 1
textField2.tag = 2
etc...

Then in your -(BOOL)textFieldShouldReturn:(UITextField *)textField you can do a:

switch (textField.tag) {
    case 1:
    labelChange1.text = textField.text;
    break;
    case 2:
    labelChange2.text = textField.text;
    break;
    etc... etc...
}

Upvotes: 3

Related Questions