I want to implement a Textfield to input your name on my SpriteKit Game, so I have something like this:
SKLabelNode* gameTitle = [SKLabelNode labelNodeWithFontNamed:#"Chalkduster"];
gameTitle.text = #"Game Title";
gameTitle.name = #"gametitle";
gameTitle.fontSize = 44.0;
gameTitle.fontColor = [SKColor blackColor];
gameTitle.position = CGPointMake(self.size.width/2,self.size.height/2 + 150);
[self addChild:gameTitle];
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(self.size.width/2, self.size.height/2+20, 200, 40)];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.textColor = [UIColor blackColor];
textField.font = [UIFont systemFontOfSize:17.0];
textField.placeholder = #"Enter your name here";
textField.backgroundColor = [SKColor whiteColor];
textField.autocorrectionType = UITextAutocorrectionTypeYes;
textField.keyboardType = UIKeyboardTypeDefault;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.delegate = self.delegate;
[self.view addSubview:textField];
so, the problem is that it's not showed on my scene, maybe the problem is when adding it into the scene, I use addSubview.
Is that wrong?
You can add objects that inherits UIView's inside the didMoveToView:(SKView *)view function
Just add that function to your SKScene and move your UITextField inside:
-(void)didMoveToView:(SKView *)view {
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(self.size.width/2, self.size.height/2+20, 200, 40)];
textField.center = self.view.center;
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.textColor = [UIColor blackColor];
textField.font = [UIFont systemFontOfSize:17.0];
textField.placeholder = #"Enter your name here";
textField.backgroundColor = [UIColor whiteColor];
textField.autocorrectionType = UITextAutocorrectionTypeYes;
textField.keyboardType = UIKeyboardTypeDefault;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.delegate = self.delegate;
[self.view addSubview:textField];
}
to remove it use the
[myTextField removeFromSuperView];
if you don't want to use the method didMoveToView that insert the textField at the scene presentation, you can use this method:
-(void)insertUI{
ttaAppDelegate *myDel = [[UIApplication sharedApplication] delegate];
UIViewController *vc = myDel.window.rootViewController;
self.textField= [[UITextField alloc] initWithFrame:CGRectMake(self.size.width/2, self.size.height/2+20, 200, 40)];
self.textField.center = self.view.center;
self.textField.borderStyle = UITextBorderStyleRoundedRect;
self.textField.textColor = [UIColor blackColor];
self.textField.font = [UIFont systemFontOfSize:17.0];
self.textField.placeholder = #"Enter your name here";
self.textField.backgroundColor = [UIColor whiteColor];
self.textField.autocorrectionType = UITextAutocorrectionTypeYes;
self.textField.keyboardType = UIKeyboardTypeDefault;
self.textField.clearButtonMode = UITextFieldViewModeWhileEditing;
self.textField.delegate = self;
self.button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.button addTarget:self action:#selector(saveScore:) forControlEvents:UIControlEventTouchDown];
[self.button setTitle:#"Save" forState:UIControlStateNormal];
self.button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[vc.view addSubview:self.textField];
[vc.view addSubview:self.button];
vc.interstitialPresentationPolicy = ADInterstitialPresentationPolicyManual;
[vc requestInterstitialAdPresentation];
}
Related
In my application dn't use autolayout and size classes. Create all the elements programmatically.
Calculate view width and set frame for each element. Here my code.
UILabel* TitleLbl = [[UILabel alloc] initWithFrame:CGRectMake((self.view.frame.size.width/2)-25,(self.view.frame.size.height/2)-235,100, 50)];
TitleLbl.text=#"Login";
TitleLbl.textColor=[UIColor whiteColor];
TitleLbl.font = [UIFont boldSystemFontOfSize:14.0];
[self.view addSubview:TitleLbl];
//NSLog(#"ttttt %#",(self.view.frame.size.height/10));
UILabel* AccountLbl = [[UILabel alloc] initWithFrame:CGRectMake((self.view.frame.size.width/2)-130,(self.view.frame.size.height/2)-210,350, 50)];
AccountLbl.text=#"Login or Create Your New Account";
AccountLbl.textColor=[UIColor whiteColor];
AccountLbl.font = [UIFont boldSystemFontOfSize:16.0];
[self.view addSubview:AccountLbl];
UITextField* UserNameFld = [[UITextField alloc] initWithFrame:CGRectMake(self.view.frame.origin.x+40, (self.view.frame.size.height/2)-175,self.view.frame.size.width-80, 40)];
UserNameFld.font = [UIFont systemFontOfSize:15];
UserNameFld.placeholder = #"Username";
UserNameFld.keyboardType = UIKeyboardTypeDefault;
NSAttributedString *str = [[NSAttributedString alloc] initWithString:#"Username" attributes:#{ NSForegroundColorAttributeName : [UIColor colorWithRed:244.0f/255.0f green:244.0f/255.0f blue:244.0f/255.0f alpha:1.0] }];
UserNameFld.attributedPlaceholder = str;
UserNameFld.delegate = self;
[self DrawLine: UserNameFld];
[self.view addSubview:UserNameFld];
UITextField* PassWorldFld = [[UITextField alloc] initWithFrame:CGRectMake(self.view.frame.origin.x+40, (self.view.frame.size.height/2)-135,self.view.frame.size.width-80, 40)];
PassWorldFld.font = [UIFont systemFontOfSize:15];
PassWorldFld.placeholder = #"Password";
PassWorldFld.keyboardType = UIKeyboardTypeDefault;
PassWorldFld.secureTextEntry = YES;
PassWorldFld.delegate = self;
NSAttributedString *strPassword = [[NSAttributedString alloc] initWithString:#"Password" attributes:#{ NSForegroundColorAttributeName : [UIColor colorWithRed:244.0f/255.0f green:244.0f/255.0f blue:244.0f/255.0f alpha:1.0] }];
PassWorldFld.attributedPlaceholder = strPassword;
[self DrawLine: PassWorldFld];
[self.view addSubview:PassWorldFld];
UIButton *ForPassBtn = [UIButton buttonWithType:UIButtonTypeCustom];
// [ForPassBtn addTarget:self action:#selector(aMethod:)
// forControlEvents:UIControlEventTouchUpInside];
[ForPassBtn setTitle:#"Forgot Password?" forState:UIControlStateNormal];
ForPassBtn.frame = CGRectMake((self.view.frame.size.width/2), (self.view.frame.size.height/2)-90, (self.view.frame.size.width/2)-40, 20.0);
ForPassBtn.layer.borderColor = [UIColor whiteColor].CGColor;
ForPassBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
ForPassBtn.titleLabel.font = [UIFont systemFontOfSize:13.0];
ForPassBtn.backgroundColor=[UIColor clearColor];
[self.view addSubview:ForPassBtn];
UIButton *LoginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
// [LoginBtn addTarget:self action:#selector(aMethod:)
// forControlEvents:UIControlEventTouchUpInside];
[LoginBtn setTitle:#"Login" forState:UIControlStateNormal];
LoginBtn.frame = CGRectMake(self.view.frame.origin.x+40,(self.view.frame.size.height/2)-65, self.view.frame.size.width-80, 30.0);
LoginBtn.layer.cornerRadius = 10;
LoginBtn.layer.borderWidth=1.0f;
LoginBtn.layer.borderColor = [UIColor whiteColor].CGColor;
LoginBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
LoginBtn.titleLabel.font = [UIFont systemFontOfSize:16.0];
[self.view addSubview:LoginBtn];
How can i calulate the dynamic heigth for this code.
Any one can help me. how to calulate dynamic heigth. give any solution for me. Thanks advance.
Write below in .pch file
#define iPhoneFactorX ([[UIScreen mainScreen] bounds].size.width*1.00/1080.0)
Now write a code as per screen size of 1080x1920
E.g. you want to set a UIImage of size 600*400
UIImageView *m1 = [[UIImageView alloc] initWithFrame:CGRectMake(240*iPhoneFactorX, 100*iPhoneFactorX, 600*iPhoneFactorX, 400*iPhoneFactorX)];
Below is how I calculate starting point as 240
(1080-600)/2
^^^ width of image
This make 1 code for all screens....
UILabel* TitleLbl = [[UILabel alloc] initWithFrame:CGRectMake((self.view.frame.size.width/2)-25,(self.view.frame.size.height/2)-235,100, 50)];
TitleLbl.text=#"Login";
TitleLbl.textColor=[UIColor whiteColor];
TitleLbl.font = [UIFont boldSystemFontOfSize:14.0];
[self.view addSubview:TitleLbl];
//NSLog(#"ttttt %#",(self.view.frame.size.height/10));
UILabel* AccountLbl = [[UILabel alloc] initWithFrame:CGRectMake((self.view.frame.size.width/2)-130,TitleLbl.frame.origin.y + TitleLbl.frame.size.height + 10,350, 50)];
AccountLbl.text=#"Login or Create Your New Account";
AccountLbl.textColor=[UIColor whiteColor];
AccountLbl.font = [UIFont boldSystemFontOfSize:16.0];
[self.view addSubview:AccountLbl];
UITextField* UserNameFld = [[UITextField alloc] initWithFrame:CGRectMake(self.view.frame.origin.x+40, AccountLbl.frame.origin.y + AccountLbl.frame.size.height + 10,self.view.frame.size.width-80, 40)];
UserNameFld.font = [UIFont systemFontOfSize:15];
UserNameFld.placeholder = #"Username";
UserNameFld.keyboardType = UIKeyboardTypeDefault;
NSAttributedString *str = [[NSAttributedString alloc] initWithString:#"Username" attributes:#{ NSForegroundColorAttributeName : [UIColor colorWithRed:244.0f/255.0f green:244.0f/255.0f blue:244.0f/255.0f alpha:1.0] }];
UserNameFld.attributedPlaceholder = str;
UserNameFld.delegate = self;
// [self DrawLine: UserNameFld];
[self.view addSubview:UserNameFld];
UITextField* PassWorldFld = [[UITextField alloc] initWithFrame:CGRectMake(UserNameFld.frame.origin.x, UserNameFld.frame.origin.y + UserNameFld.frame.size.height + 10,UserNameFld.frame.origin.x + UserNameFld.frame.size.width, 40)];
PassWorldFld.font = [UIFont systemFontOfSize:15];
PassWorldFld.placeholder = #"Password";
PassWorldFld.keyboardType = UIKeyboardTypeDefault;
PassWorldFld.secureTextEntry = YES;
PassWorldFld.delegate = self;
NSAttributedString *strPassword = [[NSAttributedString alloc] initWithString:#"Password" attributes:#{ NSForegroundColorAttributeName : [UIColor colorWithRed:244.0f/255.0f green:244.0f/255.0f blue:244.0f/255.0f alpha:1.0] }];
PassWorldFld.attributedPlaceholder = strPassword;
// [self DrawLine: PassWorldFld];
[self.view addSubview:PassWorldFld];
UIButton *ForPassBtn = [UIButton buttonWithType:UIButtonTypeCustom];
// [ForPassBtn addTarget:self action:#selector(aMethod:)
// forControlEvents:UIControlEventTouchUpInside];
[ForPassBtn setTitle:#"Forgot Password?" forState:UIControlStateNormal];
ForPassBtn.frame = CGRectMake((self.view.frame.size.width/2), PassWorldFld.frame.origin.y + PassWorldFld.frame.size.height + 50, (self.view.frame.size.width/2)-40, 20.0);
ForPassBtn.layer.borderColor = [UIColor whiteColor].CGColor;
ForPassBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
ForPassBtn.titleLabel.font = [UIFont systemFontOfSize:13.0];
ForPassBtn.backgroundColor=[UIColor clearColor];
[self.view addSubview:ForPassBtn];
UIButton *LoginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
// [LoginBtn addTarget:self action:#selector(aMethod:)
// forControlEvents:UIControlEventTouchUpInside];
[LoginBtn setTitle:#"Login" forState:UIControlStateNormal];
LoginBtn.frame = CGRectMake(self.view.frame.origin.x+40,ForPassBtn.frame.origin.y + ForPassBtn.frame.size.height + 50, self.view.frame.size.width-80, 30.0);
LoginBtn.layer.cornerRadius = 10;
LoginBtn.layer.borderWidth=1.0f;
LoginBtn.layer.borderColor = [UIColor whiteColor].CGColor;
LoginBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
LoginBtn.titleLabel.font = [UIFont systemFontOfSize:16.0];
[self.view addSubview:LoginBtn];
Im trying to show up a pop up containing a UIPickerView and UITextField.
Following is my code that I use to create the pop up.
self.alert = [UIAlertController alertControllerWithTitle:nil message:#"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" preferredStyle:UIAlertControllerStyleAlert];
CGRect pickerFrame = CGRectMake(0, 0, 270, 200);
UIPickerView *regionsPicker = [[UIPickerView alloc] initWithFrame:pickerFrame];
regionsPicker.tag = 1;
regionsPicker.dataSource = (id <UIPickerViewDataSource>)self;
regionsPicker.delegate = (id <UIPickerViewDelegate>) self;
[regionsPicker setShowsSelectionIndicator:YES];
UIView *toolView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 270.0f, 44.f)];
toolView.backgroundColor = [UIColor blackColor];
CGRect buttonFrame = CGRectMake(0, 5, 100, 30);
UIButton *button = [[UIButton alloc] initWithFrame: buttonFrame];
button.imageEdgeInsets = UIEdgeInsetsMake(0, button.titleLabel.frame.size.width, 0, -button.titleLabel.frame.size.width);
UIImage *btnImage = [UIImage imageNamed:#"delete.png"];
[button setImage:btnImage forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button setTitleColor: [UIColor blueColor] forState: UIControlStateNormal];
[toolView addSubview:button];
[button addTarget:self action:#selector(closeDialog:) forControlEvents:UIControlEventTouchUpInside];
self.textField = [[UITextView alloc] initWithFrame:CGRectMake(30, 170, 220, 200)];
_textField.editable = NO;
_textField.hidden=true;
self.textField.text = #"Enter text...";
self.textField.font = [UIFont systemFontOfSize:15];
self.textField.autocorrectionType = UITextAutocorrectionTypeNo;
self.textField.keyboardType = UIKeyboardTypeDefault;
self.textField.returnKeyType = UIReturnKeyDone;
self.textField.textColor = [UIColor lightGrayColor];
self.textField.delegate = (id <UITextViewDelegate>) self;
self.textField.layer.borderWidth = 1.0f;
self.textField.layer.cornerRadius = 5;
self.textField.layer.borderColor = [[UIColor grayColor] CGColor];
[toolView addSubview:self.textField];
CGRect actionFrame = CGRectMake(80, 400, 100, 30);
UIButton *actionBtn = [[UIButton alloc] initWithFrame: actionFrame];
[actionBtn setTitle:#"Submit" forState:UIControlStateNormal];
[actionBtn setTitleColor: [UIColor whiteColor] forState: UIControlStateNormal];
[actionBtn setBackgroundColor:[UIColor purpleColor]];
[self.textField setUserInteractionEnabled:YES];
[toolView setUserInteractionEnabled:YES];
[self.alert.view setUserInteractionEnabled:YES];
[self.alert.view addSubview:regionsPicker];
[self.alert.view addSubview:self.textField];
[self.alert.view addSubview:toolView];
[self.alert.view addSubview:actionBtn];
id rootViewController=[UIApplication sharedApplication].delegate.window.rootViewController;
if([rootViewController isKindOfClass:[UINavigationController class]])
{
rootViewController=[((UINavigationController *)rootViewController).viewControllers objectAtIndex:0];
}
[rootViewController presentViewController:_alert animated:YES completion:nil];
It works fine.However, if Im trying to enter text in the UITextField by tapping it, the keyboard doesn't appear and hence Im not be able to type anything within the UITextField. How can I sort this out?
the reason is you added textField, button, all those inside toolview , but the toolview height is very less
change your toolView height from 44
into 500 or else (combination of textfield, button + 50).
Change below statement
UIView *toolView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 270.0f, 44.f)];
to
UIView *toolView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 270.0f, 500.f)];
Update
change this line
[self presentViewController:alert animated:YES completion:nil];
into
id rootViewController=[UIApplication sharedApplication].delegate.window.rootViewController;
if([rootViewController isKindOfClass:[UINavigationController class]])
{
rootViewController=[((UINavigationController *)rootViewController).viewControllers objectAtIndex:0];
}
[rootViewController presentViewController:alert animated:YES completion:nil];
Update New
UIViewController *rootViewController=[UIApplication sharedApplication].delegate.window.rootViewController;
if([rootViewController isKindOfClass:[UINavigationController class]])
{
rootViewController=[((UINavigationController *)rootViewController).viewControllers objectAtIndex:0];
}else
{
while (rootViewController.presentedViewController) {
rootViewController = rootViewController.presentedViewController;
}
}
[rootViewController presentViewController:alert animated:YES completion:nil];
Swift
var rootViewController: UIViewController = UIApplication.sharedApplication().delegate.window.rootViewController
if (rootViewController is UINavigationController.self) {
rootViewController = ((rootViewController as! UINavigationController)).viewControllers[0]
}
else {
while rootViewController.presentedViewController {
rootViewController = rootViewController.presentedViewController
}
}
rootViewController.presentViewController(alert, animated: true, completion: { _ in })
Try this it may help you:-
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:#" Your message " preferredStyle:UIAlertControllerStyleAlert];
UIView *toolView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 270, 200)];
toolView.backgroundColor = [UIColor blackColor];
CGRect buttonFrame = CGRectMake(0, 5, 100, 30);
UIButton *button = [[UIButton alloc] initWithFrame:buttonFrame];
[button setTitle:#"Image" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[toolView addSubview:button];
UITextField *txtField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 270, 44)];
txtField.borderStyle = UITextBorderStyleRoundedRect;
txtField.backgroundColor = [UIColor grayColor];
txtField.font = [UIFont systemFontOfSize:15];
txtField.placeholder = #"enter text";
txtField.autocorrectionType = UITextAutocorrectionTypeNo;
txtField.keyboardType = UIKeyboardTypeDefault;
txtField.returnKeyType = UIReturnKeyDone;
txtField.clearButtonMode = UITextFieldViewModeWhileEditing;
txtField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
txtField.delegate = (id<UITextFieldDelegate>)self;
[toolView addSubview:txtField];
[txtField setUserInteractionEnabled:YES];
[toolView setUserInteractionEnabled:YES];
[alertController.view setUserInteractionEnabled:YES];
[alertController.view addSubview:toolView];
[self presentViewController:alertController animated:YES completion:nil];
i have code
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(18, 350, 309, 35)];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.font = [UIFont systemFontOfSize:15];
textField.keyboardType = UIKeyboardTypeDefault;
textField.returnKeyType = UIReturnKeyDone;
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
textField.delegate = self;
[self.view addSubview:textField];..i tried like this..
but i want
when i click add button ,each time one text field will be added to my view controller with different positions...
Thanks in Advance.....
Try this
if(![self.view viewWithTag:44]){
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(18, 350, 309, 35)];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.font = [UIFont systemFontOfSize:15];
textField.keyboardType = UIKeyboardTypeDefault;
textField.returnKeyType = UIReturnKeyDone;
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
textField.delegate = self;
textField.tag = 44;
[self.view addSubview:textField];
}
I'm trying to add a simple text field to the google map view. The textfield is getting displayed, but i can't write/type anything inside it. The keyboard don't show up either.. please help me fix this problem.
this is what i'm doing:
//Add text field
UITextField *textField = [[UITextField alloc] init];
textField.frame = CGRectMake(mapView_.bounds.size.width - 290, mapView_.bounds.size.height - 48, 180, 40);
textField.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin;
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.font = [UIFont systemFontOfSize:15];
textField.placeholder = #"Testing...";
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.keyboardType = UIKeyboardTypeDefault;
textField.returnKeyType = UIReturnKeyDone;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.delegate = self;
textField.userInteractionEnabled = YES;
[self.view addSubview: textField];
self.view.insertSubview(TextField, aboveSubview: self.mapView)
I have created 2 UITextFields and a UIButton programmatically, when I click the UIButton I want it to change the text of the highlighted UITextField but it's changing the text of the second UITextField not the highlighted one.
-(void)viewDidLoad
{
txtf = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 586, 45)];
txtf.borderStyle = UITextBorderStyleNone;
txtf.backgroundColor = [UIColor clearColor];
txtf.font = [UIFont systemFontOfSize:17];
txtf.autocorrectionType = UITextAutocorrectionTypeNo;
txtf.keyboardType = UIKeyboardTypeDefault;
txtf.returnKeyType = UIReturnKeyDone;
txtf.textAlignment = NSTextAlignmentLeft;
txtf.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[self.view addSubview:txtf];
txtf = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 586, 345)];
txtf.borderStyle = UITextBorderStyleNone;
txtf.backgroundColor = [UIColor clearColor];
txtf.font = [UIFont systemFontOfSize:17];
txtf.autocorrectionType = UITextAutocorrectionTypeNo;
txtf.keyboardType = UIKeyboardTypeDefault;
txtf.returnKeyType = UIReturnKeyDone;
txtf.textAlignment = NSTextAlignmentLeft;
txtf.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[self.view addSubview:txtf];
}
- (IBAction)change:(id)sender
{
txtf.text = #"the text was changed"
}
is there any way to solve this problem?
Change your code to
-(void)viewDidLoad{
txtf1 = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 586, 45)];
txtf1.borderStyle = UITextBorderStyleNone;
txtf1.backgroundColor = [UIColor clearColor];
txtf1.font = [UIFont systemFontOfSize:17];
txtf1.autocorrectionType = UITextAutocorrectionTypeNo;
txtf1.keyboardType = UIKeyboardTypeDefault;
txtf1.returnKeyType = UIReturnKeyDone;
txtf1.textAlignment = NSTextAlignmentLeft;
txtf1.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[self.view addSubview:txtf1];
txtf2 = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 586, 345)];
txtf2.borderStyle = UITextBorderStyleNone;
txtf2.backgroundColor = [UIColor clearColor];
txtf2.font = [UIFont systemFontOfSize:17];
txtf2.autocorrectionType = UITextAutocorrectionTypeNo;
txtf2.keyboardType = UIKeyboardTypeDefault;
txtf2.returnKeyType = UIReturnKeyDone;
txtf2.textAlignment = NSTextAlignmentLeft;
txtf2.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[self.view addSubview:txtf2];
}
- (IBAction)change:(id)sender {
txtf2.text = #"the text was changed"
}
And add txtf2 to class members.
UPDATE:
To change highlighted UITextField modify - (IBAction)change:(id)sender to
- (IBAction)change:(id)sender
{
if ( txtf1.isHighlighted ) {
txtf1.text = #"the text 1 was changed";
}
else if ( txtf2.isHighlighted ) {
txtf2.text = #"the text 2 was changed";
}
else {
NSLog(#"none of the textField is Highlighted");
}
}
UPDATE 2
When you have a lot of text fields you can try following
for (id subview in self.view.subviews) {
if ([subview isKindOfClass:[UITextField class]]) {
UITextField *textField = subview;
if (textField.isHighlighted) {
textField.text = #"the text was changed";
}
}
}
txtf always points to second textField. so thats the problem.
When you created the first UItextFied, txtf points to it. But when you create a new UITextField and make txtf point to it, then reference with first textField is lost.
And in your change method, you are always getting the reference of second textfield and changing it.
Two options,
1) use txtf1 and txtf2, two class properties
2) use tags. [UIView viewWithTag:tagValue];
Using Tag's->
Assuming numberOfTextFields you using is 3.
txtf = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 586, 45)];
//set its properties
txtf.tag=1;
[self.view addSubview:txtf];
txtf = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 586, 45)];
//set its properties
txtf.tag=2;
[self.view addSubview:txtf];
txtf = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 586, 45)];
//set its properties
txtf.tag=3;
[self.view addSubview:txtf];
Modify change method to
- (IBAction)change:(id)sender
{
NSInteger numberOfTextFields=3;
UITextField *textField;
for (int i=1; i<=numberOfTextFields; i++) {
textField=(UITextField*)[self.view viewWithTag:i];
if(textField.isHighlighted==YES)
break;
else
textField=nil;
}
if(textField==nil){
NSLog(#"none is highlightes");
}
else{
textField.text= #"new text for highlighted textField";
}
}
Because your both UITextField has same name (txtf), So first change the name of either first or second UITextField. Other wise you code is correct. :)
EDITED :
Just make more correct #Avt's updated answer.
- (IBAction)change:(id)sender
{
if ( txtf1.isHighlighted ) {
txtf1.text = #"the text 1 was changed";
}
else if ( txtf2.isHighlighted ) {
txtf2.text = #"the text 2 was changed";
}
else {
NSLog(#"none of the textField is Highlighted");
}
}