textBox is not firstResponder even though it is the only textBox I have picked - ios7

As soon as I run the program, I select the first textBox and then select a second textBox. fNameDone runs when textBox 1 touchUpOutside.The NSLogs are there to check if the textbox is the first responder and both nslogs return false. But it is the only textBox I have picked. My ultimate goal is to have each text box I pick, the keyboard goes away when I touchUpOutside.
- (IBAction)fNameDone:(id)sender
{
if([_firstName isEditing])
NSLog(#"True");
else
NSLog(#"False");
[self.view endEditing:TRUE];
if([_firstName isEditing])
NSLog(#"True");
else
NSLog(#"False");
}

Try this (there is no textbox in iOS I think you mean TextField)
- (BOOL)textFieldShouldReturn:(UITextField*)aTextField
{
if(aTextField.tag == 100){
[pickerView setHidden:YES];
return NO;
}
[aTextField resignFirstResponder];
return YES;
}
Don't forgot to set the delegate of textfield
EDIT:
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
if(aTextField.tag == 100){
firstField.inputVIew = pickerView ;
}
}

Related

Change UITextField background when editing begins for multiple fields

So I found this thread a while ago:
Change UITextField background when editing begins
The top answer is a fantastic resource for changing the background image of a textField when the user edits it. However, my issue is, how can you enable this with MULTIPLE text fields?
Specifically (for my app) I have a login and password field. Using the code I'll post below I can get my first text field to change images correctly when the user taps it. However, I cannot get the next field to follow suit when the user taps either the next text field or the "Next" option on the keyboard. As you can only have one instance of "textFieldShouldBeginEditing" as well as ending, you need to set the code for image change for both fields under the same section. however, when I do this, both my fields change when the first field is tapped, and both restore when the second is tapped.
Any ideas on this one excellent community?
Here's some code:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
_userNameTextField.background = [UIImage imageNamed:#"login_field_highlighted#2x"];
return YES;
_passwordTextField.background = [UIImage imageNamed:#"password_field_highlighted#2x"];
return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
_userNameTextField.background = [UIImage imageNamed:#"login_field#2x"];
return YES;
_passwordTextField.background = [UIImage imageNamed:#"password_field#2x"];
return YES;
}
- (BOOL) textFieldShouldReturn:(UITextField *)textField {
NSLog(#"textFieldShouldReturn");
if (textField == _userNameTextField) {
[_passwordTextField becomeFirstResponder];
} else if (textField == _passwordTextField) {
[_passwordTextField resignFirstResponder];
}
return YES;
}
In each of those methods, you just need to check to see which textfield is targeted, just like you did in textFieldShouldReturn:
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
if (textField == _userNameTextField) {
_userNameTextField.background = [UIImage imageNamed:#"login_field#2x"];
} else if (textField == _passwordTextField) {
_passwordTextField.background = [UIImage imageNamed:#"password_field#2x];
}
return YES;
}

Go to next textfield when hit Return

I wrote a textFieldDone: method that's suppose to move the cursor to the next textfield when the Return button is tapped.
- (IBAction)textFieldDone:(id)sender {
[nextTextField becomeFirstResponder];
NSLog(#"in : textFieldDone");
}
I have connected the first textfield's "Did End On Exit" event to the File's Owner and chose the textFieldDone: method.
I also assigned the File's Owner as the textfield's delegate (because I need the view to scroll up/down accordingly so the keyboard won't hide the textfields).
When I run the app on the simulator and tap the return button the first textfield resign first responder and in the log I see that the program didn't go through the textFieldDone: method, but it did go through the textFieldDidEndEditing: method.
I used that method before and had no problem.
Is it because the File's Owner is the textfield's delegate?
You need to write on
- (BOOL) textFieldShouldReturn:(UITextField*) textField
to go to next text field.
Sample code:
-(BOOL) textFieldShouldReturn:(UITextField*) textField
{
if (textField == txt1)
{
[txt1 resignFirstResponder];
[txt2 becomeFirstResponder];
}
if (textField == txt2)
{
[txt2 resignFirstResponder];
}
return YES;
}
Don't forget to add delegate UITextFieldDelegate to your UITextfield.
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if ([textField isEqual:txt1])
{
[txt2 becomeFirstResponder];
}
return true;
}
the above answers are correct, but to make this more general you should use the tag option
UITextField *txt1;
txt1.tag=1;
UITextField *txt2;
txt2.tag=2;
UITextField *txt3;
txt3.tag=3;
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if ([[textField superview] viewWithTag:textField.tag+1])
{
[[[textField superview] viewWithTag:textField.tag+1] becomeFirstResponder];
}
else{ [textField resignFirstResponder];
}
return true;
}
note: don't use textField with tag 0. because all subViews have tag=0 by default.

Make NSTableView active

Cocoa noob here.
I've got a simple Mac app that includes an NSTextField that fetches some results and puts them into an NSTableView. I'd like to be able to press up/down while in the text field to activate the first/last item in the table view.
I've done the following:
- (void)keyUp:(NSEvent *)theEvent {
switch([theEvent keyCode]) {
case 125: {
NSLog(#"I need to move down");
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:0];
[resultsTableView selectRowIndexes:indexSet byExtendingSelection:NO];
[resultsTableView becomeFirstResponder];
break;
}
case 126: {
NSLog(#"I need to move up");
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:[results count]-1];
[resultsTableView selectRowIndexes:indexSet byExtendingSelection:NO];
[resultsTableView becomeFirstResponder];
break;
}
}
}
Which is partially what I want. It does select the first or last item in the resultsTableView, but the selected item stays grayed out, and the textview stays active.
I thought calling becomeFirstResponder on the resultsTableView would do the trick, but it didn't.
Any help would be greatly appreciated!
Never call becomeFirstResponder directly. Use NSWindow's makeFirstResponder:
[[resultsTableView window] makeFirstResponder:resultsTableView];

navigate through textfields

I'm trying to use the #PeyloW code that I found here How to navigate through textfields (Next / Done Buttons) but when I press the keyboard return button nothing happens. My tags are ok.
Header:
- (BOOL)textFieldShouldReturn:(UITextField*)textField;
Implementation:
- (BOOL)textFieldShouldReturn:(UITextField*)textField {
NSInteger nextTag = textField.tag + 1;
UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];
if (nextResponder) {
[nextResponder becomeFirstResponder];
} else {
[textField resignFirstResponder];
}
return NO;
}
What am I missing? My keyboard doesn't have next done, only return. Keep in mind that I'm very new to iOS.
EDIT:
I tried to debug the code by adding a breakpoint to it and the code isn't being triggered.
I don't like solutions that incorporate the tag. Instead I would put all inputfileds in the desired order into an array and in -textFieldShouldReturn: use the given textfield to get it's index from in the array. Then I would get the object at that index.
- (BOOL)textFieldShouldReturn:(UITextField*)textField {
NSUInteger nextIndex = [arrayWithResponders indexOfObject:textField]+1 % [arrayWithResponders count];
UIResponder* nextResponder = [arrayWithTextFields objectAtIndex: nextIndex];
if (nextResponder) {
[nextResponder becomeFirstResponder];
} else {
[textField resignFirstResponder];
}
return NO;
}
You just added, that the breakpoints aren't triggered, so most likely you didn't set up the delegate.

How do I get a UITextField to accept focus without showing the keyboard?

I am trying to find a way to prevent the keyboard from appearing when the user taps on a TextField but could`t find a way to do it.
I tried this code after I linked my textField to delegate and still it did not work for me, the code was logging but the keyboard did appear.
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
NSLog(#"BeginEditing");
return YES;
[textField resignFirstResponder];
}
When I return with NO I lose the focus from the textField, which I need.
The textFields I have are filled with values from a buttons on the same view, thats why I don't want the keyboard to appear, and at the same time I want the user to select the textField they want to fill.
if you just want user to select the textfield to fill and does not want to keyboard to show up then you can do the following:
add tag to your textfields
change the code to this:
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
selectedTextFieldTag = textField.tag;
return NO;
}
use selectedTextField value to identify which textfield to fill in your code. return NO will not allow keyboard to appear.
This will help you for sure.
-(BOOL)textFieldShouldBeginEditing:(UITextField*)textField {
UIView *dummyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
activeField.inputView = dummyView; // Hide keyboard, but show blinking cursor
return YES;
}
I tested and it is working for me. Hope this will be useful for others who have similar issue.
[textField resignFirstResponder]; will not be called because you are returning from the method before it can get called. Does that not fire a warning?
Try returning NO here or if that doesn't work, try disabling user-interaction on the text field:
[myTextField setUserInteractionEnabled:NO];
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
NSLog(#"BeginEditing");
[textField resignFirstResponder];
return YES;
}
so here you just use flag int variable to assign the value to focused textfield
define int i; flag globally in .h or .m file
after that in textField Delegate method use bellow code...
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
if (textField == yourtextField1 ) {
i=1;
}
else if (textField == yourtextField2 ) {
i=2;
}
else if (textField == yourtextField3 ) {
i=3;
}
else if (textField == yourtextField4 ) {
i=4;
}
return NO;
}
-(IBAction)yourbutton1_Clicked:(id)sender{
if( i == 1){
yourtextField1.text=yourbutton1.titleLabel.text;
}
else if ( i == 2){
yourtextField2.text=yourbutton1.titleLabel.text;
}
else if ( i == 3){
yourtextField3.text=yourbutton1.titleLabel.text;
}
else if ( i == 4){
yourtextField4.text=yourbutton1.titleLabel.text;
}
else{
NSLog(#"Please Click On TextField");//here you can put AlertView Message
}
}
and so on.......
also you can use common method with sender id of button and also tag......