添加UIToolbar在一些文本字段上input附件视图

在我寻求我的第一个iPhone应用程序,我已经发布了关于在iOS键盘上处理返回键的正确方法。 现在我需要用prev / next和donebutton来找出键盘上方的工具栏。 我一直在使用以下站点的示例: input附件视图

该示例工程,但我想使用UIToolbar和UIBarButtonItem的,而不是button的常规视图。 我尝试了各种组合,但都没有工作。 这是我到目前为止。

标题:

#import <UIKit/UIKit.h> @interface ViewController : UIViewController <UITextFieldDelegate> { UITextField* txtActiveField; UIToolbar* keyboardToolbar; UIBarButtonItem* btnDone; UIBarButtonItem* btnNext; UIBarButtonItem* btnPrev; } @property (nonatomic, strong) IBOutlet UITextField* firstNameTextField; @property (nonatomic, strong) IBOutlet UITextField* lastNameTextField; @property (nonatomic, strong) IBOutlet UITextField* cityTextField; @property (nonatomic, strong) UITextField* txtActiveField; @property (nonatomic, strong) UIToolbar* keyboardToolbar; @property (nonatomic, strong) UIBarButtonItem* btnDone; @property (nonatomic, strong) UIBarButtonItem* btnNext; @property (nonatomic, strong) UIBarButtonItem* btnPrev; -(void)createInputAccessoryView; @end 

资源:

 #import "ViewController.h" @implementation ViewController @synthesize firstNameTextField = _firstNameTextField; @synthesize lastNameTextField = _lastNameTextField; @synthesize cityTextField = _cityTextField; @synthesize txtActiveField = _txtActiveField; @synthesize keyboardToolbar = _keyboardToolbar; @synthesize btnDone = _btnDone; @synthesize btnNext = _btnNext; @synthesize btnPrev = _btnPrev; - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } #pragma mark - View lifecycle - (void)viewDidLoad { [super viewDidLoad]; [self createInputAccessoryView]; } - (void)viewDidUnload { [self setFirstNameTextField:nil]; [self setLastNameTextField:nil]; [self setCityTextField:nil]; [self setTxtActiveField:nil]; [self setKeyboardToolbar:nil]; [self setBtnDone:nil]; [self setBtnNext:nil]; [self setBtnPrev:nil]; [super viewDidUnload]; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return YES; } -(void)gotoPrevTextfield { if (self.txtActiveField == self.firstNameTextField) { return; } else if (self.txtActiveField == self.lastNameTextField) { [self.firstNameTextField becomeFirstResponder]; } else if (self.txtActiveField == self.cityTextField) { [self.lastNameTextField becomeFirstResponder]; } } -(void)gotoNextTextfield { if (self.txtActiveField == self.firstNameTextField) { [self.lastNameTextField becomeFirstResponder]; } else if (self.txtActiveField == self.lastNameTextField) { [self.cityTextField becomeFirstResponder]; } else if (self.txtActiveField == self.cityTextField) { return; } } -(void)doneTyping { [self.txtActiveField resignFirstResponder]; } -(void)createInputAccessoryView { self.keyboardToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)]; self.keyboardToolbar.barStyle = UIBarStyleBlackTranslucent; self.keyboardToolbar.tintColor = [UIColor darkGrayColor]; btnPrev = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:@selector(gotoPrevTextfield:)]; btnNext = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:@selector(gotoNextTextfield:)]; btnDone = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneTyping:)]; [self.keyboardToolbar addSubview:(UIView*)btnPrev]; [self.keyboardToolbar addSubview:(UIView*)btnNext]; [self.keyboardToolbar addSubview:(UIView*)btnDone]; [self.firstNameTextField setInputAccessoryView:self.keyboardToolbar]; [self.lastNameTextField setInputAccessoryView:self.keyboardToolbar]; [self.cityTextField setInputAccessoryView:self.keyboardToolbar]; } -(void)textFieldDidBeginEditing:(UITextField*)textField { self.txtActiveField = textField; } @end 

当我启动应用程序时,我得到:

 012-01-21 09:31:19.798 KeyboardToolbar[14772:f803] -[UIBarButtonItem superview]: unrecognized selector sent to instance 0x6b19350 2012-01-21 09:31:19.799 KeyboardToolbar[14772:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIBarButtonItem superview]: unrecognized selector sent to instance 0x6b19350' *** First throw call stack: (0x13bc052 0x154dd0a 0x13bdced 0x1322f00 0x1322ce2 0x5042f 0x4a72b 0x3422 0x2a58 0xd964e 0x39a73 0x39ce2 0x39ea8 0x40d9a 0x11be6 0x128a6 0x21743 0x221f8 0x15aa9 0x12a6fa9 0x13901c5 0x12f5022 0x12f390a 0x12f2db4 0x12f2ccb 0x122a7 0x13a9b 0x2728 0x2685 0x1) terminate called throwing an exception 

我的假设是我没有正确地分配一个元素。 我也想知道是否需要在button的类中有字段,或者是否可以将这些字段添加到工具栏中。 我在标题中放了一行,这样我就不必把所有的方法都放在文件的开头。 不知道尚未转发的语法,就像在C中可以做的那样声明一个方法。

谢谢。

更新:

所以我对createInputAccessoryView做了一些修改。 我现在使用addItems来添加button。 我做了局部variables的button(但是和以前一样有问题)。 好消息是,应用程序不会崩溃了,坏消息是工具栏显示,但没有任何button。 不知道还有什么我需要做的,以实现显示button。 我见过的所有例子都是相似的。

 -(void)createInputAccessoryView { self.keyboardToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)]; self.keyboardToolbar.barStyle = UIBarStyleBlackTranslucent; self.keyboardToolbar.tintColor = [UIColor darkGrayColor]; UIBarButtonItem* previousButton = [[UIBarButtonItem alloc] initWithTitle:@"Previous" style:UIBarButtonItemStyleBordered target:self action:@selector(gotoPrevTextfield)]; UIBarButtonItem* nextButton = [[UIBarButtonItem alloc] initWithTitle:@"Next" style:UIBarButtonItemStyleBordered target:self action:@selector(gotoNextTextfield)]; UIBarButtonItem* flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneTyping:)]; [keyboardToolbar setItems:[NSArray arrayWithObjects: previousButton, nextButton, flexSpace, doneButton, nil] animated:NO]; [self.firstNameTextField setInputAccessoryView:self.keyboardToolbar]; [self.lastNameTextField setInputAccessoryView:self.keyboardToolbar]; [self.cityTextField setInputAccessoryView:self.keyboardToolbar]; } 

UIBarButtonItem不是UIView子类,所以不能将其作为UIToolbar的子视图添加。 相反,使用工具栏上的– setItems:animated:方法来添加这些button。

不知道是否为时已晚。 我把我的解决scheme添加到UIKeyboard的UIToolbar和键盘types更改与UITextField的内容,并处理UIBarButtonItem与“上一页”,“下一页”和“完成”在Github 。 如果在tableview的不同部分的TextFields,它变得更加棘手。

我已经设法在我当前的项目中使用你的代码,除了在UITableView中由文本字段启动的UIDatePicker的顶部添加工具栏。 我正在使用静态单元格。

这个对我有用。

我有UITextFields作为IBOutlets,只使用此代码来设置InputAccessoryView

 [textFieldName setInputAccessoryView:self.keyboardToolbar]; [textFieldEmail setInputAccessoryView:self.keyboardToolbar]; [textFieldPhone setInputAccessoryView:self.keyboardToolbar]; [textFieldDate setInputAccessoryView:self.keyboardToolbar]; [textFieldSize setInputAccessoryView:self.keyboardToolbar]; 

那么,next和prevbutton的function就像一个魅力(但只有在桌子上)。 回去时,我仍然不得不手动将表格滚动到顶部。 出于某种原因,UITableViewController只处理滚动的情况,我发现这很奇怪,但这里是我以前的代码片段。

 - (void) gotoPrev { if (self.activeField == textFieldName) { // name --> nowhere return; } else if (self.activeField == textFieldEmail) { // email --> name [textFieldName becomeFirstResponder]; } else if (self.activeField == textFieldPhone) { // phone --> email [textFieldEmail becomeFirstResponder]; // Scroll jeegar NSIndexPath * myIndexPath= [NSIndexPath indexPathForRow:0 inSection:0]; [self.tableView scrollToRowAtIndexPath:myIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES]; } else if (self.activeField == textFieldDate) { // date --> phone [textFieldPhone becomeFirstResponder]; } else if (self.activeField == textFieldSize) { // people --> date [textFieldDate becomeFirstResponder]; } } 

我认为你的button是不可见的,因为下面的代码:

 self.keyboardToolbar.barStyle = UIBarStyleBlackTranslucent; self.keyboardToolbar.tintColor = [UIColor darkGrayColor]; 

尝试使用

 self.keyboardToolbar.barStyle = UIBarStyleBlack; self.keyboardToolbar.tintColor = [UIColor whiteColor]; 

代替

问题必须出现在添加子辅助视图的地方。