Let me apologize so many tags because I feel like there are more than one way to make it work and I am not sure what is causing the problem.
I have a UICollectionView, in the cell I have a textfield. The text field uses only num pad and I added a cancel and done button using solutions posted in How to show "Done" button on iPhone number pad.
The code is
- (void)viewDidLoad
{
[super viewDidLoad];
UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
numberToolbar.barStyle = UIBarStyleBlackTranslucent;
numberToolbar.items = @[[[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelNumberPad)],
[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc]initWithTitle:@"Apply" style:UIBarButtonItemStyleDone target:self action:@selector(doneWithNumberPad)]];
[numberToolbar sizeToFit];
numberTextField.inputAccessoryView = numberToolbar;
}
-(void)cancelNumberPad{
[numberTextField resignFirstResponder];
numberTextField.text = @"";
}
-(void)doneWithNumberPad{
NSString *numberFromTheKeyboard = numberTextField.text;
[numberTextField resignFirstResponder];
}
I modified the code in done to just
-(void)doneWithNumberPad{
[self.view endEditing:YES];
}
Now I want to apply the cancel button to make the textfield that triggered the toolbar's textfield to become the textfield.placeholder.
I've tried to pass in the textfield in the cancelNumPad and it didn't work. So I tried the approach to get the selected cell from the textfield invoked. Usually when I do an IBAction I can just declare the sender and use superview to get the selected cell, but since the barbutton is in a toolbar, I've had no luck.
The set up of my controller is as follows. Note the UItextfield is declared under implementation.
@interface MenuViewController () <UITextFieldDelegate>{
UITextField *myTextField;
}
then I init the textfield, the textfield value is init to be 0.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
static NSString *reuseIdentifier = @"Cell2";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
// Configure the cell
myTextField = (UITextField*)[cell viewWithTag:400;
[[myTextField layer] setBorderWidth:1.0f];
[[myTextField layer] setBorderColor:[UIColor blackColor].CGColor];
myTextField.text = [arrayOfQuantities objectAtIndex:indexPath.item];
myTextField.delegate = self;
myTextField.inputAccessoryView = numberToolbar;
}
return cell;
}
Here are my delegate code
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField{
BOOL valid;
BOOL max;
NSCharacterSet *alphaNums = [NSCharacterSet decimalDigitCharacterSet];
NSCharacterSet *inStringSet = [NSCharacterSet characterSetWithCharactersInString:textField.text];
valid = [alphaNums isSupersetOfSet:inStringSet];
//maximum of 100
if([textField.text intValue]>=100){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Can't be more than 100"
message:@"Please adjust number"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
max = NO;
}
else{
max = YES;
}
if (valid && max){
return YES;
}//not valid
else{
return NO;
}
}
-(void)textFieldDidEndEditing:(UITextField *)textField{
if (textField.text.length == 0) {
textField.text = textField.placeholder;
}
textField.placeholder = @"";
UITextField *currentTextField = textField;
UICollectionViewCell *selectedCell = (UICollectionViewCell *)[[currentTextField superview]superview];
UICollectionView *collectionViewForSelection = (UICollectionView *)[selectedCell superview];
NSIndexPath *textFieldIndexPath = [collectionViewForSelection indexPathForCell:selectedCell];
textField.text = [NSString stringWithFormat:@"%d",[textField.text intValue]];
[arrayOfQuantities replaceObjectAtIndex:textFieldIndexPath.item withObject:[NSString stringWithFormat:@"%d",[currentTextField.text intValue]]];
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
textField.placeholder = textField.text;
textField.text = @"";
}
How can I get the cancel button to work?? The done button work just fine.