ios 添加 UIToolbar 以在某些文本字段上输入附件视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8954331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding UIToolbar to input accessory view on some text fields
提问by Tyrel Van Niekerk
In my quest for my first iPhone app I have posted about the correct way to handle the return key on the iOS keyboard. Now I need to figure out the toolbar above the keyboard with prev/next and done buttons. I have been working with an example from the following site: Input Accessory View
在我寻求我的第一个 iPhone 应用程序时,我已经发布了有关处理 iOS 键盘上返回键的正确方法的信息。现在我需要找出带有上一个/下一个和完成按钮的键盘上方的工具栏。我一直在使用以下站点的示例: 输入附件视图
The example works, but I want to use a UIToolbar and UIBarButtonItem's instead of just a regular view with buttons. I have tried various combinations, none work yet. Here is what I have so far.
该示例有效,但我想使用 UIToolbar 和 UIBarButtonItem 而不是带有按钮的常规视图。我尝试了各种组合,还没有奏效。这是我到目前为止所拥有的。
Header:
标题:
#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
Source:
来源:
#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
When I start the application I get:
当我启动应用程序时,我得到:
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
My assumption is that I have not allocated one of the elements correctly. I also wonder if I need to have fields in the class for the buttons or if those can just be added to the toolbar. And I put a line in the header so that I would not have to put all my methods at the beginning of the file. Don't know the syntax yet to forward declare a method like you could do in C.
我的假设是我没有正确分配元素之一。我还想知道我是否需要在类中为按钮添加字段,或者是否可以将这些字段添加到工具栏。我在标题中放了一行,这样我就不必将所有方法都放在文件的开头。不知道语法来转发声明一个方法,就像你可以在 C 中做的那样。
Thanks.
谢谢。
Update:
更新:
So I made some changes to the createInputAccessoryView. I am using addItems now to add the buttons. I made the button variables local (but had the same problem when they were as before). Good news is that the app does not crash anymore, bad news is that the toolbar shows, but without any buttons. Not sure what else I need to do to get the buttons to actually show. All the examples I have seen are similar.
所以我对 createInputAccessoryView 做了一些改动。我现在使用 addItems 添加按钮。我将按钮变量设为本地(但与以前一样存在相同的问题)。好消息是应用程序不再崩溃,坏消息是工具栏显示,但没有任何按钮。不知道我还需要做什么才能让按钮真正显示出来。我见过的所有例子都是相似的。
-(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];
}
采纳答案by Justin Voss
UIBarButtonItem
isn't a UIView
subclass, so you can't add it as a subview of the UIToolbar
. Instead, use the – setItems:animated:
method on your toolbar to add those buttons.
UIBarButtonItem
不是UIView
子类,因此您不能将其添加为UIToolbar
. 相反,使用– setItems:animated:
工具栏上的方法添加这些按钮。
回答by Liangjun
Not sure if it's too late. I put my solution about adding a UIToolbar into UIKeyboard, and The keyboard types changes with the UITextField content, and handling the UIBarButtonItem with "Previous", "Next" and "Done" at Github. And if the TextFields at different sections of the tableview, it turns to be more tricky.
不知道是不是太晚了。我把我的解决方案增加一个UIToolbar到UIKeyboard,并与内容的UITextField键盘类型的变化,以及处理与的UIBarButtonItem“上一页”,“下一页”和“完成”在Github上。如果在 tableview 的不同部分使用 TextFields,它就会变得更加棘手。
回答by mdebeus
I think your buttons are just not visible because of the following code:
由于以下代码,我认为您的按钮不可见:
self.keyboardToolbar.barStyle = UIBarStyleBlackTranslucent;
self.keyboardToolbar.tintColor = [UIColor darkGrayColor];
Try using
尝试使用
self.keyboardToolbar.barStyle = UIBarStyleBlack;
self.keyboardToolbar.tintColor = [UIColor whiteColor];
instead
反而
回答by bendigi
I have managed to use your code in my current project with the addition of adding the toolbar on top of a UIDatePicker that gets initiated by a textfield inside a UITableView. I'm using static cells.
我已经设法在我当前的项目中使用您的代码,并在 UIDatePicker 顶部添加了工具栏,该工具栏由 UITableView 中的文本字段启动。我正在使用静态单元格。
It works for me.
这个对我有用。
I have the UITextFields as IBOutlets and only use this code to set the InputAccessoryView
我有 UITextFields 作为 IBOutlets 并且只使用此代码来设置 InputAccessoryView
[textFieldName setInputAccessoryView:self.keyboardToolbar];
[textFieldEmail setInputAccessoryView:self.keyboardToolbar];
[textFieldPhone setInputAccessoryView:self.keyboardToolbar];
[textFieldDate setInputAccessoryView:self.keyboardToolbar];
[textFieldSize setInputAccessoryView:self.keyboardToolbar];
Well, the next and prev buttons work like a charm (but only on the way down the table). I still had to manually scroll the table to the top when going back up. For some reason the UITableViewController only handles the scrolling down scenario, which I found quite odd, but here is my code snippet for previous.
好吧,next 和 prev 按钮就像一个魅力(但只在下桌子的路上)。返回时,我仍然必须手动将表格滚动到顶部。出于某种原因, 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];
}
}
回答by bendigi
The problem must be in the place from where you add this sub-accessory view.
问题必须出在您添加此子附件视图的位置。