I feel bad asking this question. I have seen it on SO a couple of times, but I am not understanding the answers. I have a custom keyboard that I made in a separate view. It worked great for one text box. But now I want to have multiple text boxes that the keyboard will enter numbers in. I have made the view controller the textfield delegate and my custom keyboard pops up when you enter either of the text boxes. However, the code that I have to set the numbers is only for one of the text boxes. So I wanted to check to see which text box was selected so I will be editing in that box. I used the method – (BOOL)textFieldShouldBeginEditing:(UITextField *)textField and tried the following code…
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if ([numberOne isFirstResponder])
{
[self.numberOne = currentTextField];
}
else if ([numberTwo isFirstResponder])
{
[self.numberTwo = currentTextField];
}
}
the currentTextField is a UItextField. I then wanted to substitute currentTextField in the following code to replace numberOne.
- (IBAction)buttonPressed:(UIButton *)sender
{
NSString *button = [sender currentTitle];
NSRange decimalpoint = [self.numberOne.text rangeOfString:@"."];
if (self.userIsEnteringANumber)
{
if ([button isEqualToString:@"."] && decimalpoint.location != NSNotFound)
{
self.numberOne.text = self.numberOne.text;
}else
self.numberOne.text = [self.numberOne.text stringByAppendingString:button];
}else
{
self.numberOne.text = button;
self.userIsEnteringANumber = YES;
}
}
However, the complier doesn’t like my line [self.numberOne = currentTextField]; I tried following some of the other advice, but unfortunately I don’t understand what they are saying. Does anyone have newbie directions for me? Thanks so much in advance.
I have fixed the compiler issue with the help from spacious. Thanks. My textFieldShouldBeginEditing method is now…
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if ([numberOne isFirstResponder])
{
self.currentTextField = self.numberOne;
}
else if ([numberTwo isFirstResponder])
{
self.currentTextField = self.numberTwo;
}
return YES;
}
and I changed the IBAction buttonPressed to the following…
- (IBAction)buttonPressed:(UIButton *)sender
{
NSString *button = [sender currentTitle];
NSRange decimalpoint = [self.currentTextField.text rangeOfString:@"."];
if (self.userIsEnteringANumber)
{
if ([button isEqualToString:@"."] && decimalpoint.location != NSNotFound)
{
self.currentTextField.text = self.currentTextField.text;
}else
self.currentTextField.text = [self.currentTextField.text stringByAppendingString:button];
}else
{
self.currentTextField.text = button;
self.userIsEnteringANumber = YES;
}
}
And now something weird happens. When I click on the first text field, the keyboard comes up but nothing happens, then I hit the second text field and editing begins in the first text field. If I click on the first text field, editing begins on the second. Any help would be appreciated!!
Well, I can answer your compiler question-
To assign a reference to currentTextField, it should be:
currentTextField = self.numberOne;
Brackets are used when sending messages (calling methods on objects).
You wrapped your assignment in brackets and the assignment is the wrong way.
(You’d currently be overwriting your references to your textFields with currentTextField)
I would need to see more code to diagnose any further! Good luck
EDIT: (these are untested changes, but you’ll get the idea)