How to Dismissing keyboard? - iphone

I do have Three TextField
For Name Phone and Email.
On selecting each textField keyboard will appears
-(void)createdTextField{
phoneField = [[UITextField alloc]initWithFrame:CGRectMake(225, 306, 90, 31)];
[phoneField setPlaceholder:REQUIRED];
[phoneField setBorderStyle:UITextBorderStyleRoundedRect];
phoneField.delegate = self;
phoneField.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;
phoneField.keyboardType = UIKeyboardTypeNumberPad;
[phoneField setTag:10];
[self.view addSubview:phoneField];
[self.view addSubview:phoneField];
nextPhoneField = [[UITextField alloc]initWithFrame:CGRectMake(325, 306, 142, 31)];
[nextPhoneField setBorderStyle:UITextBorderStyleRoundedRect];
nextPhoneField.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;
nextPhoneField.delegate = self;
nextPhoneField.keyboardType = UIKeyboardTypeNumberPad;
[nextPhoneField setTag:11];
[self.view addSubview:nextPhoneField];
nameField = [[UITextField alloc] initWithFrame:CGRectMake(225, 265, 242, 31)];
[nameField setPlaceholder:REQUIRED];
[nameField setBorderStyle:UITextBorderStyleRoundedRect];
nameField.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;
// [nameField setAutocorrectionType:UITextAutocorrectionTypeNo];
[nameField setTag:12];
[self.view addSubview:nameField];
eMailField = [[UITextField alloc]initWithFrame:CGRectMake(225, 347, 242, 31)];
[eMailField setPlaceholder:REQUIRED];
[eMailField setBorderStyle:UITextBorderStyleRoundedRect];
eMailField.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;
eMailField.keyboardType = UIKeyboardTypeEmailAddress;
[eMailField setAutocorrectionType:UITextAutocorrectionTypeNo];
[eMailField setTag:13];
[self.view addSubview:eMailField];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return NO;
}
#When to dismiss keyboard when typing return button in all four textField I need to dismiss the keyboard.
Its happen only for the phone no the remaining TextField.

I saw the code you paste.
There is no delegate of the last two textfields.
I think this is the reason.

Any time you want the keyboard hidden for a give field just do:
[myField resignFirstResponder];
As apparently myField is the field currently active.

How to Dismiss UITextField Keypad
[textField resignFirstResponder];
If you want to dismiss when user touch 'return' button, use this (Needed to set delegate and protocol, )
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}

after creation of urEachTextField
urEachTextField = [[UITextField alloc] initWithFrame:CGRectMake(225, 265, 242, 31)];
urEachTextField.delegate=self;
head file .h
#interface urViewController : UIViewController<UITextFieldDelegate>

Related

Resign keyboard of textfield present in alertview

I have three text fields on an alertview, I set keyboard as decimalType ,
- (IBAction)heightMethod:(id)sender
{
self.utextfield = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)]; utextfield.placeholder = #" Centimeters";
self.utextfield.delegate=self;
self.utextfield.tag=3;
[ self.utextfield setBackgroundColor:[UIColor whiteColor]];
[self.alertHeight addSubview: self.utextfield];
// Adds a password Field
self.ptextfield = [[UITextField alloc] initWithFrame:CGRectMake(40, 80.0, 80, 25.0)]; ptextfield.placeholder = #" Feet";
self.ptextfield.delegate=self;
self.ptextfield.tag=4;
[self.ptextfield setBackgroundColor:[UIColor whiteColor]];
[self.alertHeight addSubview:self.ptextfield];
self.ptextfieldInches = [[UITextField alloc] initWithFrame:CGRectMake(140, 80.0, 80, 25.0)]; ptextfieldInches.placeholder = #" Inches";
self.ptextfieldInches.delegate=self;
self.ptextfieldInches.tag=5;
[ptextfieldInches setBackgroundColor:[UIColor whiteColor]];
[self.alertHeight addSubview:ptextfieldInches];
[self.utextfield setKeyboardType:UIKeyboardTypeDecimalPad];
[self.ptextfieldInches setKeyboardType:UIKeyboardTypeDecimalPad];
[self.ptextfield setKeyboardType:UIKeyboardTypeDecimalPad];
[self.alertHeight show];
}
As I tap any textfield, keyboard resign only two times, but on third time its not resigning . I added resignfirst responder method inside the delgate method of alertview , look here
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
Create iVar of UITextField and assign in side UITextFieldDelegate method textFieldShouldBeginEditing. Hopefully it should work.
Like Below:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
textField = iVar;
}
Try
- (void) alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
[self.view endEditing: YES];
}
When you Press OK button on alert view the keypad dismisses automatically.I have tested it out so remove resignfirstresponder from the above mentioned method and try

Creating custom UITextView with UILabels as well as text in it

I would like to create a custom UITextView with the ability to enter text in it, as well as programmatically adding some UILabels there that would act like text (I need to delete them with the 'backspace' button when the cursor comes near them).
This UITextView should be expandable and the labels can have different width.
Any ideas of how you can create this sort of thing, any tutorials or such?
you can create textfield using this code.
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 200, 300, 40)];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.font = [UIFont systemFontOfSize:15];
textField.placeholder = #"enter text";
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.keyboardType = UIKeyboardTypeDefault;
textField.returnKeyType = UIReturnKeyDone;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
textField.delegate = self;
[self.view addSubview:textField];
[textField release];
And for creating label you can use this code:
CGRect labelFrame = CGRectMake( 10, 40, 100, 30 );
UILabel* label = [[UILabel alloc] initWithFrame: labelFrame];
[label setText: #"My Label"];
[label setTextColor: [UIColor orangeColor]];
label.backgroundColor =[UIColor clearColor];
[view addSubview: label];
and for deleting label when backspace tape use this method:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
if ([string isEqualToString:#""]) {
NSLog(#"backspace button pressed");
[label removeFromSuperview];
}
return YES;
}
If the backspace button is pressed, then the replacementString(string) will have null value. So we can identify backspace button press using this.

how to acces the textfield value created manually from a Click on Button

i am doing sample application where i am creating two TextField programitically i.e,Textfield1 and Textfield2 and Having one Button which is Dragged on UI.
Now My requirement is I have two textfield created manually and a Button, When i click a button it should access the value entered in both textfield and display its value on console using NSLog.
so can anyone suggest me how to do it?As textifeild is created programmitically it is not having IBOutlet. so i want to read textfield value when i click button and display on Console.so please suggest me with sample code
{
[self Textfiled1];
[Self TextField2];
}
-(void)textField1
{
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(200, 250, 200, 35)];
textField.returnKeyType = UIReturnKeyDone;
textField.adjustsFontSizeToFitWidth = TRUE;
[textField addTarget:self
action:#selector(textFieldDone:)
forControlEvents:UIControlEventEditingDidEndOnExit];
[self.view addSubview:textField];
[textField release];
}
Same as above for Textfield2.
Set tag for both the UITextField.
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(200, 250, 200, 35)];
textField.returnKeyType = UIReturnKeyDone;
textField.tag = 1;
...
for the second,
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(200, 250, 200, 35)];
textField.returnKeyType = UIReturnKeyDone;
textField.tag = 2;
...
In Button click action method,
UITextField *textField = (UITextField*)[self.view viewWithTag:1];
NSString *text1 = textField.text;
Similarly for the next UITextField.
Set the text field' s tag property before adding it and get the text field back using it.
textField.tag = 1;
UITextField* tf1 = (UITextField*)[self.view viewWithTag:1];
when you are adding textfield to self.view use the tag like below.
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(200, 250, 200, 35)];
textField.returnKeyType = UIReturnKeyDone;
textField.adjustsFontSizeToFitWidth = TRUE;
[textField addTarget:self
action:#selector(textFieldDone:)
forControlEvents:UIControlEventEditingDidEndOnExit];
textField.tag = 3;
[self.view addSubview:textField];
[textField release];
whenever you need it then do like this.
UITextField *tex = [self.view viewWithTag:3];
NSLog(#"%#",tex.text);

Dynamic UITextfield doesnt respond to keyboard inputs

HI all below is the code i am using, the textField is created in .h file and when i run the code the text field doesnt respond to keyboard( no letters typed are visible) also when i click Done on keybpard it does not trigger textfieldshouldreturn method. please help!!!
UIActionSheet *aac =[[UIActionSheet alloc] initWithTitle:#"New list item" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
UIDatePicker *theDatePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0.0,100.0,0.0,0.0)];
self.textField=[[UITextField alloc] init];
self.textField.frame = CGRectMake(12, 45, 260, 25);
[self.textField setDelegate:self];
self.textField.textColor=[UIColor blackColor];
self.textField.backgroundColor=[UIColor whiteColor];
[textField setBorderStyle:UITextBorderStyleRoundedRect];
[textField setReturnKeyType:UIReturnKeyDone];
textField.keyboardType=UIKeyboardAppearanceDefault;
[aac addSubview:textField];
[aac addSubview:theDatePicker];
[textField release];
[theDatePicker release];
UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:#"Done"]];
closeButton.momentary=YES;
closeButton.frame = CGRectMake(270, 7.0f, 50.0f, 30.0f);
closeButton.segmentedControlStyle=UISegmentedControlStyleBar;
closeButton.tintColor =[UIColor blackColor];
[closeButton addTarget:self action:#selector(dismissActionSheet) forControlEvents:UIControlEventValueChanged];
[aac addSubview:closeButton];
[closeButton release];
[aac showInView:self.view];
[aac setBounds:CGRectMake(0, 0, 320, 685)];
Have you tried removing the self. from before textField? If you have defined it in your header, as well as used #property, and #synthesize in your .m, you should be good. Also, make sure you have the <UITextFieldDelegate> in your interface.
I just had the same problem; My textfield did bring up the keyboard when I touched it, but no letters appeared in the textfield when tapping on the keyboard.
In my case I had been messing with the AppDelegate.m and removed the [self.window makeKeyAndVisible];. So putting it back again (in application:didFinishLaunchingWithOptions:) solved it for me.

Keyboard not dissmissing

In my view controller i added a text field as below.
txt1 = [[UITextField alloc]initWithFrame:CGRectMake(10, 10, 70, 20)];
txt1.delegate = self;
txt1.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:txt1];
[txt1 release];
I set UITextFieldDelegate in .h file. I write
-(BOOL)textFieldShouldReturn:(UITextField *)textField
to dissmiss my keyboard.But it not firing delegate method.
Any help!!!
Thanks in advance.
Have you tried sending the resignFirstResponder message?
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}