如何从工具栏中的栏按钮的 UICollection 字段中获取(索引)文本字段 xcode ios

How to get (index of) textfield from in UICollectionField from the barbutton in toolbar xcode ios

让我道歉这么多标签,因为我觉得有不止一种方法可以让它工作,我不确定是什么导致了这个问题。

我有一个 UICollectionView,在单元格中有一个文本字段。文本字段仅使用数字键盘,我使用 How to show "Done" button on iPhone number pad 中发布的解决方案添加了一个取消和完成按钮。

密码是

- (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];
}

我将 done 中的代码修改为

-(void)doneWithNumberPad{
[self.view endEditing:YES];
}

现在我想应用取消按钮使触发工具栏文本字段的文本字段成为 textfield.placeholder。

我试过在 cancelNumPad 中传入文本字段,但没有成功。所以我尝试了从调用的文本字段中获取选定单元格的方法。通常当我执行 IBAction 时,我可以只声明发件人并使用 superview 来获取选定的单元格,但是由于 barbutton 在工具栏中,我没有运气。

我的控制器设置如下。请注意,UItextfield 是在实现中声明的。

@interface MenuViewController () <UITextFieldDelegate>{
UITextField *myTextField;
}

然后我初始化文本字段,文本字段值初始化为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;
}

这是我的委托代码

-(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 = @"";
}

我怎样才能让取消按钮起作用??完成按钮工作正常。

对于那些正在寻找答案的人。在你的 .m 文件中, UITextField *currentTextField;

在文本字段委托中

-(void)textFieldDidBeginEditing:(UITextField *)textField{
currentTextField = textField;
UICollectionViewCell *selectedCell = (UICollectionViewCell *)[[currentTextField    superview]superview];
UICollectionView *collectionViewForSelection = (UICollectionView *) [selectedCell superview];
NSIndexPath *textFieldIndexPath = [collectionViewForSelection  indexPathForCell:selectedCell];
//do whatever with your cell after