Resign keyboard of textfield present in alertview - iphone

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

Related

How to step through multiple UITextFields

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.

Adding buttons to viewForFooterSection UITableView

after some discussion on this topic I'm attemting to implement 2 buttons on the footerview on a UItableView. To create the buttons, I declare them on the declaration .h file, synthesized on the implementation .m file and then, I created them by code, like this:
- (UIButton *)resetButton{
if (resetButton == nil)
{
resetButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
resetButton.frame = CGRectMake(20.0, 40 , 95.0, 37.0);
[resetButton setTitle:#"Reset" forState:UIControlStateNormal];
resetButton.backgroundColor = [UIColor clearColor];
[resetButton addTarget:self action:#selector(resetAction:) forControlEvents:UIControlEventTouchDown];
resetButton.tag = 1;
}
return resetButton;
}
and then, I implemented this code into the UITableView delegate method:
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 100.0)];
[customView addSubview:self.resetButton];
[customView addSubview:self.calculateButton];
return [customView autorelease];
}
by doing so, the buttons appear on the screen however, when I tap them, nothing happens (I implemented a AlertView on the actions to check if they work.
any help here?
Thanks!
EDIT: the actions linked to the button:
-(IBAction)resetAction:(id)sender {
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"Reset"
message:#"You just pressed the Reset button"
delegate:self
cancelButtonTitle:#"Acknowledged"
otherButtonTitles:nil];
[alert show];
[alert release];
}
I think what you are missing is - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section.
Without this the custom footer will be height 0. The reason you can still see the button is because clipsToBounds is NO by default for the custom view.

How to Dismissing keyboard?

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>

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.

wait_fences: failed to receive reply: 10004003

I have an alertview that is being created with a textfield inside of it. when i close the alertview via a submit or cancel button, i am getting the wait_fences error in the console. it doesnt crash, or i havent been able to make it crash but id really like to figure out what is going on.
alert = [[UIAlertView alloc]
initWithTitle:#"Lookup"
message:#"\n\n\n"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Submit", nil];
label = [[UILabel alloc] initWithFrame:CGRectMake(12, 40, 260, 25)];
label.font = [UIFont systemFontOfSize:16];
label.textColor = [UIColor whiteColor];
label.backgroundColor = [UIColor clearColor];
label.shadowColor = [UIColor blackColor];
label.shadowOffset = CGSizeMake(0, -1);
label.textAlignment = UITextAlignmentCenter;
label.text = #"Enter 10-Digit ISBN Number";
[alert addSubview:label];
field = [[UITextField alloc] initWithFrame:CGRectMake(16, 83, 252, 25)];
field.font = [UIFont systemFontOfSize:18];
field.backgroundColor = [UIColor whiteColor];
field.keyboardAppearance = UIKeyboardAppearanceAlert;
field.keyboardType = UIKeyboardTypeNumberPad;
field.borderStyle = UITextBorderStyleBezel;
field.delegate = self;
[field becomeFirstResponder];
[alert addSubview:field];
[alert show];
I looked around online to try and figure out what the problem was and some people had mentioned resigningFirst responder. I added that to - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex but it didnt do anything.
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
[field resignFirstResponder]
if (buttonIndex == 1) {
NSLog(#"Submit");
} else {
NSLog(#"Cancel");
}
}
I then added an if statement to try and find out if field was the first responder and i got nothing.
if([field isFirstResponder]) {
NSLog(#"field isFirstResponder");
}
does anyone have any suggestions of what i could have done wrong?
so i think ive got it figured out.
it seems that when i was calling [field resignFirstResponder] from alertView:didDismissWithButtonIndex, firstResponder wouldnt really resign and the keyboard would be forced out a second or two after the alertView had been removed. but when i call [field resignFirstResponder] from alertView:clickedButtonAtIndex it would resign firstResponder like it is supposed to.
I also ran into a problem were the alertview would move down before being removed, to compensate for the keyboard not being there, but to fix this i created a function thats sole purpose is to call [field resignFirstResponder] and called that from alertView:clickedButtonAtIndex and delayed it being run by .2 seconds.
Did you try removing textfield like:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
[[alertView.subviews objectAtIndex:0] removeFromSuperview];
}