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;
}
Related
I have four UITextFields that I would like to use in a UIAlertView, Currently this is what it is looking like
What I would like to be able to do is restrict each box to 4 characters once each field once that 4 character limit is reached then I would like the next UITextField to become the first responder.
I would also like to be able to do this in reverse so if characters are being deleted once there are no characters available in the second field go to the first and start deleting etc.
Currently my code is pretty sloppy as I am just trying to get something like this working.. so this is what it looks like so far.
//prompt user to enter registration here
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Please Register Device" message:#" " delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12, 45, 53, 30)];
UITextField *myTextField1 = [[UITextField alloc] initWithFrame:CGRectMake(77, 45, 53, 30)];
UITextField *myTextField2 = [[UITextField alloc] initWithFrame:CGRectMake(142, 45, 53, 30)];
UITextField *myTextField3 = [[UITextField alloc] initWithFrame:CGRectMake(207, 45, 53, 30)];
// need to do something with first reasponder once 4 digits has been entered per box etc.
[myTextField becomeFirstResponder];
myTextField.placeholder =#"AAAA";
myTextField.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;
myTextField.textAlignment = NSTextAlignmentCenter;
myTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
myTextField1.placeholder =#"AAAA";
myTextField1.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;
myTextField1.textAlignment = NSTextAlignmentCenter;
myTextField1.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
myTextField2.placeholder =#"AAAA";
myTextField2.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;
myTextField2.textAlignment = NSTextAlignmentCenter;
myTextField2.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
myTextField3.placeholder =#"AAAA";
myTextField3.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;
myTextField3.textAlignment = NSTextAlignmentCenter;
myTextField3.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[myTextField setBackgroundColor:[UIColor whiteColor]];
[myTextField1 setBackgroundColor:[UIColor whiteColor]];
[myTextField2 setBackgroundColor:[UIColor whiteColor]];
[myTextField3 setBackgroundColor:[UIColor whiteColor]];
[alert addSubview:myTextField];
[alert addSubview:myTextField1];
[alert addSubview:myTextField2];
[alert addSubview:myTextField3];
[alert show];
Any help would be greatly appreciated.
// UPDATE:
this is how I am trying to use the delegate method
//.h
<UITextFieldDelegate>
//.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//..
[self.myTextField setDelegate:self];
[self.myTextField1 setDelegate:self];
[self.myTextField2 setDelegate:self];
[self.myTextField3 setDelegate:self];
//..
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSLog(#"yay");
return NO;
}
Update
I tried the code below, and does work, but for a reason if you change the FirstResponder, the text won't get written. So I modified the function. You should do this:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if(textField.text.length >= MAX_LENGTH)
return NO;
if((textField.text.length + string.length) >= MAX_LENGTH)
{
int currentTag = textField.tag;
if(currentTag == 4)
return YES;
UITextField *newTextField = (UITextField *) [textField.superview viewWithTag:(currentTag+1)];
[newTextField becomeFirstResponder];
textField.text = [textField.text stringByAppendingString:string];
}
return YES; //you probably want to review this...
}
Any way, this doesn't work when you wan't to delete content, but that's easy solved if you change the code to check the range of the changes.
I made a sample project, you can get it here: TextFieldTest
This is how I would do it, using the textfield delegate, you can check the new inserted characters, if you reach the maximum, you move to the next textfield:
static int MAX_LENGTH = 4;
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if((textField.text.length + string.length) >= MAX_LENGTH)
{
//Get next TextField... A simple way to do this:
UITextField *newTextField = [textField.superview viewWithTag:(textField.tag+1)];
[newTextField becomeFirstResponder];
//remember to set the tags in order
}
return YES; //you probably want to review this...
}
(Sorry if dosen't compile I just wrote it here directly)
Good Luck!
You can set delegate for these text fields and implement your business logic (set one of the text fields as the new first responder) in the delegate methods.
I've added my UITextField and UITextView inside a UIWindow I can not able to type inside in both of them. Both are not letting me allow to type in those fields. And, can't able to dismiss it when return pressed in keyboard.
-(void)showAlert
{
alertWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
alertView = [[UIView alloc] initWithFrame:CGRectMake(10, 115, 300, 230)];
alertWindow.windowLevel = UIWindowLevelAlert; // puts it above the status bar
alertView.center = CGPointMake(alertWindow.frame.size.width/2, alertWindow.frame.size.height/2);
alertView.backgroundColor = [UIColor whiteColor];
alertView.layer.borderWidth = 1.0;
alertView.layer.borderColor = [[UIColor whiteColor]CGColor];
UITextField *txt = ....
....
....
txt.delegate = self;
...
...
[alertView addSubview:txt];
UITextView *txtview = ...
....
....
txtview.delegate = self;
...
[alertView addSubview:txtview];
[alertWindow addSubview:alertView];
[alertWindow addSubviewWithZoomInAnimation:alertView duration:1.0 option:curveValues[0]];
[alertWindow setHidden:NO];
}
I've declared both of the delegate methods also. I placed a breakpoint there and checked also, even that method not yet called.
Update -
textFieldShouldReturn delegate method
- (BOOL) textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
[reserveView resignFirstResponder];
[reserveWindow resignFirstResponder];
[self.reserveWindow endEditing:YES];
[reserveView endEditing:YES];
return YES;
}
You should try this for dismiss the keyboard from the UIWindow
[self.window endEditing:YES];
Source
I found, that method has not been called because of UIWindow So, i changed my UIWindow to UIView And, i fixed this issue by using UIView+Animation.h from here It provides the animation what i required exactly.
- (void) addSubviewWithZoomInAnimation:(UIView*)view duration:(float)secs option:(UIViewAnimationOptions)option;
- (void) removeWithZoomOutAnimation:(float)secs option:(UIViewAnimationOptions)option;
Thanks to Google! Cheers!
The keyboard will never dismiss on its own (even if you hit return!). You'll notice that in your delegate methods, you will receive events whenever you press return/done/etc.
A simple way to make the keyboard dismiss every time someone hits the return key is to implement the - (BOOL)textViewShouldEndEditing:(UITextView *)textView method.
- (BOOL)textViewShouldEndEditing:(UITextView *)textView
{
[textView endEditing:YES];
return YES;
}
Edit: In your post you edited to say that you've declared both of the delegate methods... there are more than two so could you clarify what methods you've implemented? http://developer.apple.com/library/ios/#documentation/uikit/reference/UITextViewDelegate_Protocol/Reference/UITextViewDelegate.html#//apple_ref/occ/intf/UITextViewDelegate
Try this
- (void)viewDidLoad
{
[super viewDidLoad];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(dismissKeyboard)];
tapGesture.cancelsTouchesInView = NO;
[self.window addGestureRecognizer:tapGesture];
//[self.view addGestureRecognizer:tapGesture];
[tapGesture release];
}
-(void)dismissKeyboard
{
[txt resignFirstResponder];
[textView resignFirstResponder];
}
Try this
That's the delegate method of textfield:-
(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[txtview resignFirstResponder];
return NO;
}
I can provide code if needed, however my problem looks fundamendal. I have a UITextField in a view that can copy and paste in it. After the action I cannot do it again. It works only once.
What might be the reason behind it? Is it possible that the paste menu is not shown because of another view in the window?
some code:
myTextField = [[UITextField alloc] initWithFrame:CGRectMake(0,1,320,50)];
[myTextField setFont:[UIFont boldSystemFontOfSize:40]];
[myTextField setTextColor:[UIColor whiteColor]];
[myTextField setText:#""];
[myTextField setBorderStyle:UITextBorderStyleNone];
[myTextField setEnabled:YES];
[myTextField setKeyboardType:UIKeyboardTypePhonePad];
[myTextField setDelegate:self];
myTextField.inputView = hiddenView;
and
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if(action == #selector(paste:))
return YES;
return NO;
}
Do I need to add something in the viewWillAppear method related with the UITextField? As I said the first time works fine.
UPDATE: After the first paste the copy/paste/select mechanism stopped working on my application in ALL views...
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if (sel_isEqual(action, #selector(paste:)))
{
return YES;
}
return [super canPerformAction:action withSender:sender];
}
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>
I've searched through this site that I couldn't find a solution to the problem I'm facing now. Hope someone can help.
I've created a UIAlertView to prompt user to enter their name in an iPhone app.
UIAlertView *enterNameAlert = [[UIAlertView alloc] initWithTitle:#"Enter your name"
message:#"\n\n\n"
delegate:self
cancelButtonTitle:NSLocalizedString(#"Cancel", nil)
otherButtonTitles:NSLocalizedString(#"OK", nil),nil];
UITextField *enterNameField = [[UITextField alloc] initWithFrame:CGRectMake(16, 83, 252, 25)];
enterNameField.keyboardAppearance = UIKeyboardAppearanceAlert;
enterNameField.borderStyle = UITextBorderStyleRoundedRect;
enterNameField.autocorrectionType = UITextAutocorrectionTypeNo;
enterNameField.clearButtonMode = UITextFieldViewModeWhileEditing;
enterNameField.returnKeyType = UIReturnKeyDone;
enterNameField.delegate = self;
[enterNameField becomeFirstResponder];
[enterNameAlert addSubview:enterNameField];
[enterNameAlert show];
[enterNameAlert release];
[enterNameField release];
I've setup this viewController to comply with UITextFieldDelegate in the header file, and implemented textFieldShouldReturn: trying to dismiss the keyboard when user hit Done.
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if ([textField isFirstResponder]) {
[textField resignFirstResponder];
NSLog(#"text field was first responder");
} else {
[textField becomeFirstResponder];
[textField resignFirstResponder];
NSLog(#"text field was not first responder");
}
NSLog(#"textfieldshouldreturn");
return YES;
}
In the debugger, I confirm that this method is called successfully when I tap on the Done button, with the textField being the first responder at that time. However, the keyboard doesn't go away, but the little clearButton goes away. It is clear that the textField is no longer the first responder because when I click the Done button again, nothing get called. I just want to dismiss the keyboard with the Done button. Can anyone please offer a solution? Million thanks.
First you need to define your enterNameAlert in interface header. then use the below code.
- (void)viewDidLoad {
[super viewDidLoad];
enterNameAlert = [[UIAlertView alloc] initWithTitle:#"Enter your name"
message:#"\n\n\n"
delegate:self
cancelButtonTitle:NSLocalizedString(#"Cancel", nil)
otherButtonTitles:NSLocalizedString(#"OK", nil),nil];
UITextField *enterNameField = [[UITextField alloc] initWithFrame:CGRectMake(16, 83, 252, 25)];
enterNameField.keyboardAppearance = UIKeyboardAppearanceAlert;
enterNameField.borderStyle = UITextBorderStyleRoundedRect;
enterNameField.autocorrectionType = UITextAutocorrectionTypeNo;
enterNameField.clearButtonMode = UITextFieldViewModeWhileEditing;
enterNameField.returnKeyType = UIReturnKeyDone;
enterNameField.delegate = self;
[enterNameAlert addSubview:enterNameField];
[enterNameField becomeFirstResponder];
[enterNameAlert show];
[enterNameField release];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if ([textField isFirstResponder]) {
[textField resignFirstResponder];
[enterNameAlert dismissWithClickedButtonIndex:1 animated:YES];
[enterNameAlert release];
NSLog(#"text field was first responder");
} else {
[textField becomeFirstResponder];
[textField resignFirstResponder];
NSLog(#"text field was not first responder");
}
NSLog(#"textfieldshouldreturn");
return YES;
}
What happens if you move the releasing of the text field to the delegate method, after you call the resignFirstResponder method?
Try dismissing keyboard using resignFirstResponder on "DidEndOnExit" event of your TextField.
Then try to dismiss keyboard by pressing Done button on keyboard when you have finished entering the data into your TextField.
Hope this helps.
Have another object become and then immediately resign the first responder.
simply do
[mytextField addTarget:self
action:#selector(methodToFire:)
forControlEvents:UIControlEventEditingDidEndOnExit];
if anyone is having trouble with this in storyboard i had to go into my uitextfields properties and ctrl+drag from its delegate to its root view controller all in storyboard and that fixed everything..