I wants add multiple UITextFields in a single loop. And identify them when user interact with UITextFields . So please help me.
Thanks
Currently I place UITextField Individually, like this
txt_FirstName=[[UITextField alloc]initWithFrame:CGRectMake(framex1, framey1, framex2, framey2)];
txt_FirstName.borderStyle = UITextBorderStyleRoundedRect;
txt_FirstName.font = [UIFont systemFontOfSize:15];
txt_FirstName.placeholder = #"First Name";
txt_FirstName.autocorrectionType = UITextAutocorrectionTypeNo;
txt_FirstName.keyboardType = UIKeyboardTypeDefault;
txt_FirstName.returnKeyType = UIReturnKeyDone;
txt_FirstName.clearButtonMode = UITextFieldViewModeWhileEditing;
txt_FirstName.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
txt_FirstName.delegate = self;
[frameView addSubview:txt_FirstName];
[txt_FirstName release];
framey1=framey1+50;
txt_MiddleName=[[UITextField alloc]initWithFrame:CGRectMake(framex1, framey1, framex2, framey2)];
txt_MiddleName.borderStyle = UITextBorderStyleRoundedRect;
txt_MiddleName.font = [UIFont systemFontOfSize:15];
txt_MiddleName.placeholder = #"Middle Name";
txt_MiddleName.autocorrectionType = UITextAutocorrectionTypeNo;
txt_MiddleName.keyboardType = UIKeyboardTypeDefault;
txt_MiddleName.returnKeyType = UIReturnKeyDone;
txt_MiddleName.clearButtonMode = UITextFieldViewModeWhileEditing;
txt_MiddleName.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
txt_MiddleName.delegate = self;
[frameView addSubview:txt_MiddleName];
[txt_MiddleName release];
Please use this, but Implement UITextField delegate in your .h file. but You should have to change your place holder according to your requirement like 1 for FirstName, 2 for MiddleName etc.
First create common function like this,
-(void)setTextfieldStyle:(UITextField *)pTmpTextField
{
pTmpTextField.borderStyle = UITextBorderStyleRoundedRect;
pTmpTextField.font = [UIFont systemFontOfSize:15];
pTmpTextField.placeholder = #"First Name";
pTmpTextField.autocorrectionType = UITextAutocorrectionTypeNo;
pTmpTextField.keyboardType = UIKeyboardTypeDefault;
pTmpTextField.returnKeyType = UIReturnKeyDone;
pTmpTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
pTmpTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
pTmpTextField.delegate = self;
}
and use this for creating UITextFiled,
int y = 0;
for(int i=0;i < 5;i++)
{
UITextField *txtTemp=[[UITextField alloc]initWithFrame:CGRectMake(0, y, 300, 31)];
txtTemp.tag = i;
[self setTextfieldStyle:txtTemp];
[self.view addSubview:txtTemp];
[txtTemp release];
y+=36;
}
This is UITextField's delegate method,
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
NSLog(#"%#",textField.text);
}
Hope this works for you..
If you want to set attributes of a bunch of text fields in a loop, you could also do something like:
NSMutableArray* allTextFields = [NSMutableArray array];
// create the textFields here, I'm assuming you need to keep individual
// references to them.
txt_FirstName=[[UITextField alloc]init];
pTmpTextField.placeholder = #"First Name";
[allTextFields addObject:txt_FirstName];
// now add the common style stuff and frame
[allTextFields enumerateObjectsUsingBlock:^(UITextField* textField, NSUInteger idx, BOOL* stop) {
textField.frame = CGRectMake(framex1, framey1 + ( 50 * idx), framex2, framey2);
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.font = [UIFont systemFontOfSize:15];
textField.placeholder = #"First Name";
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.keyboardType = UIKeyboardTypeDefault;
textField.returnKeyType = UIReturnKeyDone;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
textField.delegate = self;
}];
Related
I am trying to get 3 different values from 3 different Uitextfields and then save them into 1 string to send it to a rest API server through a button action. I am stucked on how to get all 3 texts inputs from the 3 UItextfields.Any thought? My textfields are account,username,password.
UITextField *account = [[UITextField alloc] initWithFrame:CGRectMake(90, 50, 140, 30)];
account.borderStyle = UITextBorderStyleRoundedRect;
account.font = [UIFont systemFontOfSize:14];
account.placeholder = #"Account";
account.autocorrectionType = UITextAutocorrectionTypeNo;
account.keyboardType = UIKeyboardTypeDefault;
account.returnKeyType = UIReturnKeyDone;
account.clearButtonMode = UITextFieldViewModeWhileEditing;
account.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
account.delegate = self;
[self.view addSubview:account];
[account release];
UITextField *username = [[UITextField alloc] initWithFrame:CGRectMake(90, 80, 140, 30)];
username.borderStyle = UITextBorderStyleRoundedRect;
username.font = [UIFont systemFontOfSize:14];
username.placeholder = #"User ID";
username.autocorrectionType = UITextAutocorrectionTypeNo;
username.keyboardType = UIKeyboardTypeDefault;
username.returnKeyType = UIReturnKeyDone;
username.clearButtonMode = UITextFieldViewModeWhileEditing;
username.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
username.delegate = self;
[self.view addSubview:username];
[username release];
UITextField *password = [[UITextField alloc] initWithFrame:CGRectMake(90, 110, 140, 30)];
password.borderStyle = UITextBorderStyleRoundedRect;
password.font = [UIFont systemFontOfSize:14];
password.placeholder = #"Password";
password.autocorrectionType = UITextAutocorrectionTypeNo;
password.keyboardType = UIKeyboardTypeDefault;
password.secureTextEntry = YES;
password.returnKeyType = UIReturnKeyDone;
password.clearButtonMode = UITextFieldViewModeWhileEditing;
password.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
password.delegate = self;
[self.view addSubview:password];
[password release];
Can't find away to get all 3 datas when using the :
-(void) textFieldDidEndEditing:(UITextField *)textField
function.
Thanks in advance!
Set textfield tag
userNameTextField.tag = 1
accountTextField.tag = 2
passwordTextField.tag = 3
Save the values in shouldChangeCharactersInRange
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
switch (textField.tag) {
case 1:
//userNameTextField
NSString *userNameTextField = [NSString stringWithFormat:#"%#%#",textField.text,string];
//save
break;
case 2:
//accountTextField
NSString *accountTextField = [NSString stringWithFormat:#"%#%#",textField.text,string];
//save
break;
case 3:
//passwordTextField
NSString *passwordTextField = [NSString stringWithFormat:#"%#%#",textField.text,string];
//save
break;
default:
break;
}
}
you should keep the UITextFields as ivars, as well as correlating NSStrings (userNameString, passwordString, accountString). then on the following method you should check which textField invoked your delegate and assign the textField's text to the correct string. you can also use the textfield tag property to differentiate between them, and then you don't need to keep them as ivars..
-(void) textFieldDidEndEditing:(UITextField *)textField{
if (textField==userName)
userNameString = textField.text;
if (textField== account)
accountString = textField.text
if (textField== password)
password String = textField.text;
}
give each text field a tag, e.g. 1, 2 and 3
in textFieldDidEndEditing: distinguish the three text fields by tag:
.
if (textField.tag == 1) {
// assign the text to the right variable
}
... etc.
If you follow this pattern, you can avoid cluttering your class with many properties.
To make the code more readable, you can use this pattern:
#define userNameTextField 1
#define accountTextField 2
#define passwordTextField 3
or
typedef enum {
userNameTextField = 1,
accountTextField,
passwordTextField
} TextFields;
Here is the code to show.
UIViewController* viewControoler = [[UIViewController alloc]init];
textfield = [[UITextField alloc]initWithFrame:CGRectMake(0,0,280,90)];
textfield.placeholder = #"word";
textfield.keyboardType = UIKeyboardTypeDefault;
textfield.returnKeyType = UIReturnKeySearch;
textfield.clearButtonMode = UITextFieldViewModeWhileEditing;
textfield.delegate = self;
textfield.userInteractionEnabled = YES;
[textfield becomeFirstResponder];
[viewControoler.view addSubview:textfield];
[[[CCDirector sharedDirector]openGLView] addSubview:viewControoler.view];
It is running well. But textfield is not editable because keyboard is not coming when I am clicking on uitextfiled.
Remove UIViewController,directly add on OpenGLView,
textfield = [[UITextField alloc]initWithFrame:CGRectMake(0,0,280,90)];
textfield.placeholder = #"word";
textfield.keyboardType = UIKeyboardTypeDefault;
textfield.returnKeyType = UIReturnKeySearch;
textfield.clearButtonMode = UITextFieldViewModeWhileEditing;
textfield.delegate = self;
textfield.userInteractionEnabled = YES;
[textfield becomeFirstResponder];
[[[CCDirector sharedDirector]openGLView] addSubview:textfield;
i would like to know configuration about UITextField standard please
- (IBAction)add:(id)sender
{
UITextField * textfieldToAdd = [[[UITextField alloc] init] autorelease];
// ... configuration code for textfield ...
[self.view addSubview:textfieldToAdd];
}
Here is a code from Apple's example UICatalog
#pragma mark -
#pragma mark Text Fields
- (UITextField *)textFieldNormal
{
if (textFieldNormal == nil)
{
CGRect frame = CGRectMake(kLeftMargin, 8.0, kTextFieldWidth, kTextFieldHeight);
textFieldNormal = [[UITextField alloc] initWithFrame:frame];
textFieldNormal.borderStyle = UITextBorderStyleBezel;
textFieldNormal.textColor = [UIColor blackColor];
textFieldNormal.font = [UIFont systemFontOfSize:17.0];
textFieldNormal.placeholder = #"<enter text>";
textFieldNormal.backgroundColor = [UIColor whiteColor];
textFieldNormal.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
textFieldNormal.keyboardType = UIKeyboardTypeDefault; // use the default type input method (entire keyboard)
textFieldNormal.returnKeyType = UIReturnKeyDone;
textFieldNormal.clearButtonMode = UITextFieldViewModeWhileEditing; // has a clear 'x' button to the right
textFieldNormal.tag = kViewTag; // tag this control so we can remove it later for recycled cells
textFieldNormal.delegate = self; // let us be the delegate so we know when the keyboard's "Done" button is pressed
// Add an accessibility label that describes what the text field is for.
[textFieldNormal setAccessibilityLabel:NSLocalizedString(#"NormalTextField", #"")];
}
return textFieldNormal;
}
- (UITextField *)textFieldRounded
{
if (textFieldRounded == nil)
{
CGRect frame = CGRectMake(kLeftMargin, 8.0, kTextFieldWidth, kTextFieldHeight);
textFieldRounded = [[UITextField alloc] initWithFrame:frame];
textFieldRounded.borderStyle = UITextBorderStyleRoundedRect;
textFieldRounded.textColor = [UIColor blackColor];
textFieldRounded.font = [UIFont systemFontOfSize:17.0];
textFieldRounded.placeholder = #"<enter text>";
textFieldRounded.backgroundColor = [UIColor whiteColor];
textFieldRounded.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
textFieldRounded.keyboardType = UIKeyboardTypeDefault;
textFieldRounded.returnKeyType = UIReturnKeyDone;
textFieldRounded.clearButtonMode = UITextFieldViewModeWhileEditing; // has a clear 'x' button to the right
textFieldRounded.tag = kViewTag; // tag this control so we can remove it later for recycled cells
textFieldRounded.delegate = self; // let us be the delegate so we know when the keyboard's "Done" button is pressed
// Add an accessibility label that describes what the text field is for.
[textFieldRounded setAccessibilityLabel:NSLocalizedString(#"RoundedTextField", #"")];
}
return textFieldRounded;
}
- (UITextField *)textFieldSecure
{
if (textFieldSecure == nil)
{
CGRect frame = CGRectMake(kLeftMargin, 8.0, kTextFieldWidth, kTextFieldHeight);
textFieldSecure = [[UITextField alloc] initWithFrame:frame];
textFieldSecure.borderStyle = UITextBorderStyleBezel;
textFieldSecure.textColor = [UIColor blackColor];
textFieldSecure.font = [UIFont systemFontOfSize:17.0];
textFieldSecure.placeholder = #"<enter password>";
textFieldSecure.backgroundColor = [UIColor whiteColor];
textFieldSecure.keyboardType = UIKeyboardTypeDefault;
textFieldSecure.returnKeyType = UIReturnKeyDone;
textFieldSecure.secureTextEntry = YES; // make the text entry secure (bullets)
textFieldSecure.clearButtonMode = UITextFieldViewModeWhileEditing; // has a clear 'x' button to the right
textFieldSecure.tag = kViewTag; // tag this control so we can remove it later for recycled cells
textFieldSecure.delegate = self; // let us be the delegate so we know when the keyboard's "Done" button is pressed
// Add an accessibility label that describes what the text field is for.
[textFieldSecure setAccessibilityLabel:NSLocalizedString(#"SecureTextField", #"")];
}
return textFieldSecure;
}
- (UITextField *)textFieldLeftView
{
if (textFieldLeftView == nil)
{
CGRect frame = CGRectMake(kLeftMargin, 8.0, kTextFieldWidth, kTextFieldHeight);
textFieldLeftView = [[UITextField alloc] initWithFrame:frame];
textFieldLeftView.borderStyle = UITextBorderStyleBezel;
textFieldLeftView.textColor = [UIColor blackColor];
textFieldLeftView.font = [UIFont systemFontOfSize:17.0];
textFieldLeftView.placeholder = #"<enter text>";
textFieldLeftView.backgroundColor = [UIColor whiteColor];
textFieldLeftView.keyboardType = UIKeyboardTypeDefault;
textFieldLeftView.returnKeyType = UIReturnKeyDone;
textFieldLeftView.clearButtonMode = UITextFieldViewModeWhileEditing; // has a clear 'x' button to the right
textFieldLeftView.tag = kViewTag; // tag this control so we can remove it later for recycled cells
// Add an accessibility label that describes the text field.
[textFieldLeftView setAccessibilityLabel:NSLocalizedString(#"CheckMarkIcon", #"")];
textFieldLeftView.leftView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"segment_check.png"]];
textFieldLeftView.leftViewMode = UITextFieldViewModeAlways;
textFieldLeftView.delegate = self; // let us be the delegate so we know when the keyboard's "Done" button is pressed
}
return textFieldLeftView;
}
Go through the reference doc for UITextField Class also try to read UITextFieldDelegate. Look for the different tasks that can be performed and see the methods, properties available for it. And use it according to your requirement.
Please take a look of the following screen shot.
alt text http://img442.imageshack.us/img442/8027/img0402k.png
Here is my code, I am using cocos2D
CGAffineTransform transform = CGAffineTransformMakeRotation(3.14159/2);
_view = [[CCDirector sharedDirector]openGLView];
// Input the user name
_nameField = [[UITextField alloc]initWithFrame:CGRectMake(130.0, 270.0, 200.0, 30.0)];
_nameField.transform = transform;
_nameField.adjustsFontSizeToFitWidth = YES;
_nameField.textColor = [UIColor blackColor];
[_nameField setFont:[UIFont fontWithName:#"Arial" size:14]];
_nameField.placeholder = #"<Enter Your Name>";
_nameField.backgroundColor = [UIColor clearColor];
_nameField.autocorrectionType = UITextAutocorrectionTypeNo;
_nameField.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;
_nameField.textAlignment = UITextAlignmentCenter;
_nameField.keyboardType = UIKeyboardTypeDefault;
_nameField.returnKeyType = UIReturnKeyDone;
_nameField.tag = 0;
_nameField.delegate = self;
_nameField.clearButtonMode = UITextFieldViewModeNever;
_nameField.borderStyle = UITextBorderStyleRoundedRect;
[_view addSubview:_nameField];
The problem is the text is aligned to the top of the text field.
I want it to be in the middle not to top. There is too much space in the bottom.
please suggest me the way.
Shouldn't this work?
_nameField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
My app has three text fields:
1) Username
// Initialization code
usernameTextField = [[UITextField alloc] initWithFrame:CGRectMake(20, 5, 280, 30)];//usernameTextField released
usernameTextField.placeholder = #"Username";
usernameTextField.textAlignment = UITextAlignmentLeft;
//Username text field
usernameTextField.backgroundColor = [UIColor clearColor];
usernameTextField.borderStyle = UITextBorderStyleRoundedRect;
usernameTextField.autocorrectionType = UITextAutocorrectionTypeNo;
usernameTextField.keyboardType = UIKeyboardTypeURL;
usernameTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
2) Password
// Initialization code
passwordTextField = [[UITextField alloc] initWithFrame:CGRectMake(20, 90, 280, 30)];
passwordTextField.placeholder = #"Password";
passwordTextField.textAlignment = UITextAlignmentLeft;
//Password text field
passwordTextField.backgroundColor = [UIColor clearColor];
passwordTextField.borderStyle = UITextBorderStyleRoundedRect;
passwordTextField.autocorrectionType = UITextAutocorrectionTypeNo;
passwordTextField.keyboardType = UIKeyboardTypeURL;
passwordTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
passwordTextField.secureTextEntry = YES;
3) Text View
textView = [[UITextView alloc] initWithFrame:CGRectMake(5, 10, 310, 180)];
textView.layer.cornerRadius = 8;
textView.clipsToBounds = YES;
textView.font = [UIFont fontWithName:#"Arial" size:16.0f];
What is the best/quick way to use these to upload tweets to twitter directly, with the login details for the first two and the Tweet being the textView?
You should look into using MGTwitterEngine. It provides everything you need
Authenticating is as easy as
MGTwitterEngine *twitterEngine = [[MGTwitterEngine alloc] initWithDelegate:self];
[twitterEngine setUsername:#"username" password:#"password"];
and then you just use
- (NSString *)sendUpdate:(NSString *)status;
to post an update