• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

[复制链接]
菜鸟教程小白 发表于 2022-12-13 09:52:06 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

让我对这么多标签表示歉意,因为我觉得有不止一种方法可以使它起作用,而且我不确定是什么导致了问题。

我有一个 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 actionselector(cancelNumberPad)],
                     [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
                     [[UIBarButtonItem alloc]initWithTitle"Apply" style:UIBarButtonItemStyleDone target:self actionselector(doneWithNumberPad)]];
[numberToolbar sizeToFit];
numberTextField.inputAccessoryView = numberToolbar;
}

-(void)cancelNumberPad{
[numberTextField resignFirstResponder];
numberTextField.text = @"";
}

-(void)doneWithNumberPad{
NSString *numberFromTheKeyboard = numberTextField.text;
[numberTextField resignFirstResponder];
}

我将完成的代码修改为
-(void)doneWithNumberPad{
[self.view endEditing:YES];
}

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

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

我的 Controller 的设置如下。注意 UItextfield 是在实现中声明的。
@interface MenuViewController () <UITextFieldDelegate>{
UITextField *myTextField;
}

然后我初始化文本字段,文本字段值初始化为 0。
- (UICollectionViewCell *)collectionViewUICollectionView *)collectionView cellForItemAtIndexPathNSIndexPath *)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;
}

这是我的委托(delegate)代码
-(BOOL)textFieldShouldEndEditingUITextField *)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"lease 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)textFieldDidEndEditingUITextField *)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)textFieldDidBeginEditingUITextField *)textField {
textField.placeholder = textField.text;
textField.text = @"";
}

我怎样才能让取消按钮工作?完成按钮工作得很好。



Best Answer-推荐答案


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

在文本字段委托(delegate)中

-(void)textFieldDidBeginEditingUITextField *)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

关于ios - 如何从工具栏 xcode ios 中的栏按钮的 UICollectionField 中获取(索引)文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32029087/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap