UITextfield and UIPickerView
in my view I have 4 textfields,I have dragged it from xib,
so when i click on first three textfields ,I want o open a keyboard and when i click on the 4th textfield only i want to open a UIPickerView
Below i wrote the code,which is opening the UIPickerView for all the text fields..
- (void)textFieldDidBeginEditing:(U开发者_如何学GoITextField *)textField{
[textField resignFirstResponder];
[self uiPickerPopUpDisplay];
}
So friends please help me out on how shall i do it..
Thanks&Regards Ranjit
Use textFieldShouldBeginEditing:
instead of textFieldDidBeginEditing:
method. Say your textFields' names are textField1
, textField2
, textField3
, textField4
. Then, in textFieldShouldBeginEditing: method.
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if (textField == textField4) {
[self uiPickerPopUpDisplay];
return NO;
}
return YES;
}
You have to give IBOutlet for all four textfield and set delegate.
- (void)textFieldDidBeginEditing:(UITextField *)textField{
if(textField == textBox4) {
[textField resignFirstResponder];
[self uiPickerPopUpDisplay];
}
}
You should check which text field it is in this method.
- (void)textFieldDidBeginEditing:(UITextField *)textField {
if( textField == theFourthTextField ) {
[textField resignFirstResponder];
[self uiPickerPopUpDisplay];
}
}
This assumes that your class has a reference to theFourthTextField
. If it doesn't you can add one and connect it to your XIB file as an IBOutlet
.
In interface:
@interface YourClass {
IBOutlet UITextField* theFourthTextField;
//...
}
@property (nonatomic, retain) IBOutlet UITextField* theFourthTextField;
@end
In implementation:
@implementation YourClass
@synthesize theFourthTextField;
//...
@end
And connect your text field to theFourthTextField
in Interface Builder.
You can save all references to your textFields and put switch operator in your function. Or you can set different tags to that textFields and check tag of UITextField textField in your function.
If you don't want to add an IBOutlet to each of the textFileds, the other way you could do it is to set and track a tag number for identifing each UITextField
. This would also work, by the way, with any UIView object like buttons, image etc. Adding a tag to something also allows you to get a reference to that object by calling [self.view viewWithTag:MY_DEFINED_TAG_ID];
which can come in handy.
In this case you would start by going in the xib file where you dragged the UITextField into the window and set each field to it's own Tag # that you can use to test the id of the field from your method. You will find a Tag field in the Attributes Inspector.
In addition to this, you could, in your .h file do a define for the tags make it easier to keep track of them. This define list could go in either yourClass.h file or a Constants.h if you have such a file in which case just make sure you #import Constants.h into yourClass.h file. I know there are other places you could define all this but that is two.
#define TEXTFIELD1_TAG 900
#define TEXTFIELD2_TAG 901
#define TEXTFIELD3_TAG 902
#define TEXTFIELD4_TAG 903
If you are going to use – textFieldShouldBeginEditing:
or any delegate method, make sure you make your class a UITextFieldDelegate
. In yourClass.h (I'll assume it's a viewController) you will need a line like this:
@interface My_ViewController : UIViewController < UITextFieldDelegate >
Then you will be able to use any of these methods in your view to track the textFields:
– textFieldShouldBeginEditing:
– textFieldDidBeginEditing:
– textFieldShouldEndEditing:
– textFieldDidEndEditing:
– textField:shouldChangeCharactersInRange:replacementString:
– textFieldShouldClear:
– textFieldShouldReturn:
But there are other options for the placement of your switch/case code depending on your workflow. If you look in you xib file you will find there are lots of events associated with your textfield that could fire any custom method you write. For example you could hook up the TouchDown event to trigger a custom method like the one below which has the switch/case code you could put anywhere you need it.
In this method I am using the #define we saw above to check the values of the tag. This way, I don't have to remember the tag number, I can just remember TEXTFIELD1_TAG
which is much easier.
- (IBAction)textFieldHasBeenTouched:(id)sender {
int textFieldTag = sender.tag
switch (textFieldTag)
{
case TEXTFIELD1_TAG:
// TextField 1
break;
case TEXTFIELD2_TAG:
// TextField 2
break;
case TEXTFIELD3_TAG:
// TextField 3
break;
case TEXTFIELD4_TAG:
// TextField 4
break;
default:
break;
}
}
Or use one of the delegate methods. In this method I assumed you didn't use the #define to help track tags. But since you set them in the xib, you know what they are.
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
switch ([textField tag])
{
case 900:
// TextField 1
break;
case 901:
// TextField 2
break;
case 902:
// TextField 3
break;
case 903:
// TextField 4
break;
default:
break;
}
return YES;
}
You must be having references to your 4 textFields right? Check if(textField == textField4) and then execute your code.
精彩评论