ios - 如何从工具栏 xcode ios 中的栏按钮的 UICollectionField 中获取(索引)文本字段
<p>让我对这么多标签表示歉意,因为我觉得有不止一种方法可以使它起作用,而且我不确定是什么导致了问题。<br><br>我有一个 UICollectionView,在单元格中我有一个文本字段。文本字段仅使用数字键盘,我使用 <a href="https://stackoverflow.com/questions/584538/how-to-show-done-button-on-iphone-number-pad" rel="noreferrer noopener nofollow">How to show "Done" button on iPhone number pad</a> 中发布的解决方案添加了取消和完成按钮.<br><br>代码是<br><pre><code>- (void)viewDidLoad
{
;
UIToolbar* numberToolbar = [initWithFrame:CGRectMake(0, 0, 320, 50)];
numberToolbar.barStyle = UIBarStyleBlackTranslucent;
numberToolbar.items = @[[initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelNumberPad)],
[initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[initWithTitle:@"Apply" style:UIBarButtonItemStyleDone target:self action:@selector(doneWithNumberPad)]];
;
numberTextField.inputAccessoryView = numberToolbar;
}
-(void)cancelNumberPad{
;
numberTextField.text = @"";
}
-(void)doneWithNumberPad{
NSString *numberFromTheKeyboard = numberTextField.text;
;
}
</code></pre><br>我将完成的代码修改为<br><pre><code>-(void)doneWithNumberPad{
;
}
</code></pre><br>现在我想应用取消按钮使触发工具栏文本字段的文本字段成为 textfield.placeholder。<br><br>我试图在 cancelNumPad 中传递文本字段,但没有奏效。所以我尝试了从调用的文本字段中获取选定单元格的方法。通常,当我执行 IBAction 时,我可以声明发件人并使用 superview 来获取所选单元格,但是由于 barbutton 在工具栏中,所以我没有运气。<br><br>我的 Controller 的设置如下。注意 UItextfield 是在实现中声明的。<br><pre><code>@interface MenuViewController () <UITextFieldDelegate>{
UITextField *myTextField;
}
</code></pre><br>然后我初始化文本字段,文本字段值初始化为 0。<br><pre><code>- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
static NSString *reuseIdentifier = @"Cell2";
UICollectionViewCell *cell = ;
// Configure the cell
myTextField = (UITextField*)[cell viewWithTag:400;
[ setBorderWidth:1.0f];
[ setBorderColor:.CGColor];
myTextField.text = ;
myTextField.delegate = self;
myTextField.inputAccessoryView = numberToolbar;
}
return cell;
}
</code></pre><br>这是我的委托(delegate)代码<br><pre><code>-(BOOL)textFieldShouldEndEditing:(UITextField *)textField{
BOOL valid;
BOOL max;
NSCharacterSet *alphaNums = ;
NSCharacterSet *inStringSet = ;
valid = ;
//maximum of 100
if(>=100){
UIAlertView *alert = [ initWithTitle:@"Can't be more than 100"
message:@"Please adjust number"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
;
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 *)[superview];
UICollectionView *collectionViewForSelection = (UICollectionView *);
NSIndexPath *textFieldIndexPath = ;
textField.text = ];
]];
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
textField.placeholder = textField.text;
textField.text = @"";
}
</code></pre><br>我怎样才能让取消按钮工作?完成按钮工作得很好。</p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p>对于那些正在寻找答案的人。在您的 .m 文件中,<br>UITextField *currentTextField;<br><br>在文本字段委托(delegate)中<br><pre><code>-(void)textFieldDidBeginEditing:(UITextField *)textField{
currentTextField = textField;
UICollectionViewCell *selectedCell = (UICollectionViewCell *)[superview];
UICollectionView *collectionViewForSelection = (UICollectionView *) ;
NSIndexPath *textFieldIndexPath = ;
//do whatever with your cell after
</code></pre></p>
<p style="font-size: 20px;">关于ios - 如何从工具栏 xcode ios 中的栏按钮的 UICollectionField 中获取(索引)文本字段,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/32029087/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/32029087/
</a>
</p>
页:
[1]