- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- 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 在工具栏中,我没有运气。
我的 Controller 设置如下。请注意,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;
}
这是我的委托(delegate)代码
-(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;
在文本字段委托(delegate)中
-(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
关于ios - 如何从工具栏xcode ios中的barbutton中的UICollectionField中获取(索引)文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32029087/
通常,当我们点击 UIBarButtonItem 时,它会突出显示。 但是,我打算向用户表明,当 View 在 4 秒后加载时,该操作会自动为他们完成。 所以我想在不点击按钮的情况下突出显示它。 我怎
一切都很好,直到我呈现一个 uialertcontroller。当我展示它时,barbutton 从它们的位置向下移动。 Before presenting,everything was fine w
在我当前的应用程序中,我有三个 UIViewControllers 并且我正在使用 StoryBoard: ViewController、MenuViewController、CategoriasLi
如何为UIImagePickerController的左右UIBarButton设置自定义字体? 我已经尝试过这段代码,但它不起作用: let imagePicker = UIImagePickerC
我的主窗口在右上角有一个导航栏和一个 BarButton。我有一个保存的值,根据它是真还是假,我想推送到不同的 Controller 。现在推送到一个只是 IB 中的 Ctrl+Drag,但是如果我想
我创建了 4 个带有图像的 BarButtons,当我添加到导航栏时。它显示了项目之间的大间隙以及如何消除图像之间的间隙。 最佳答案 您可以通过像这样维护图像插图来实现此目的:- UIBarButto
我在我的应用中发现了这个问题。 可以同时触摸“BackBarButton”和“rightBarButton”。然后将调用这两个 Action 。 我知道UIView中有“exclusiveTouch”
关闭。这个问题需要details or clarity .目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 9 年前。 Improve t
我有一个 ViewController。应用程序中有一个 NavigationController。当我将 Bar Button Item 拖到导航栏时,该项目通常会到达 ViewController
我正在使用实用程序应用程序的模板。在 FlipSideViewController 中,我为 UINavigationController navController 添加了 IBOutlet。在代码
我有问题,我已经将 UIPickerView BarButton 标题颜色设置为蓝色,但它没有提供完美的输出,但是当我单击该按钮时,它提供了输出意味着它改变了颜色为蓝色,但我不希望只有在单击后才显示蓝
我有一个 UISplitViewController,其中一个 UIViewController 作为主 Controller ,一个 UINavigationController 作为我的细节 Co
我想在主从应用程序模板中使用自定义 UIButton 而不是 BarButton。 请多多指教。 谢谢!! 最佳答案 您可以通过使用 initWithCustomView 在 UIBarButtonI
我有一个包含三个选项卡的 tabBarController。我只想在第一个 ViewController 中将 rightBarButtonItem 显示到导航栏,但是当我回来时我希望它再次可见。如何
我是一名优秀的程序员,十分优秀!