问题描述
我有一个带有添加单元格...行的UITableView,并且希望像在消息"应用程序中那样弹出一个键盘,上方有一个视图,用户可以在其中键入新单元格的名称./p>
我意识到还有其他几种获取用户数据的方法,也有几种方法来实现我试图实现的功能.
例如,当应创建新的播放列表时,iPod应用程序将显示一个弹出窗口.
现在,我有一个隐藏的文本字段,当按下添加单元格..."行时,它被设置为第一响应者,并且我将包含输入字段和确认按钮的视图分配为该隐藏字段的inputAccessoryView.或者,我可以将此视图添加为表的子视图,并使用键盘通知将其定位.
我只想知道是否有一种更干净的方法来完成我想做的事情(例如,将要显示的输入textField的inputAccessoryView设置为textField的超级视图).我尝试了几种方法,但是在关闭视图时,似乎无法使用resignFirstResponder消除键盘.我可以使inputAccessoryView消失,但是键盘仍然保持绝对打开状态,占用了必要的屏幕空间.另外,当笔尖的视图是一个以UITableViewController作为文件所有者的UITableView而不是以UIViewController作为文件所有者的UIView时,我得到一个非常奇怪的错误:设置表的第一个响应者视图,但我们不知道其类型(单元格/页眉/页脚)"
预先感谢,朱利安·塞佩克(Julian Ceipek)
使用UITextView/UITextField类上的 setInputAccessoryView
,您处在正确的轨道上.此方法可让您添加所需的任何视图在键盘顶部.然后,您创建的视图将使用委托方法来告诉主视图控制器resignFirstResponder.
所以,让您开始吧:
@protocol TextFieldAccessoryViewDelegate@必需的-(void)keyboardShouldDismiss;@结尾@interface TextFieldAccessoryView:UIView@属性(非原子的,保留)id< TextFieldAccessoryViewDelegate>代表;-(id)initWithFrame:(CGRect)具有withDelegate的框架(id< TextFieldAccessoryViewDelegate>)aDelegate;@结尾
实现可能看起来有点像(仅发布构成视图的代码):
#pragma标记-私有方法-(void)doneButtonTapped:(id)sender{[委托keyboardShouldDismiss];}-(void)setUpChildrenView{UIBarButtonItem * doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"样式:UIBarButtonItemStyleDone目标:自我操作:@selector(doneButtonTapped :)];UINavigationItem * navigationItem = [[UINavigationItem alloc] initWithTitle:@"];[navigationItem setRightBarButtonItem:doneButton];[navigationItem setHidesBackButton:YES];UINavigationBar * navigationBar = [[[[UINavigationBar alloc] initWithFrame:CGRectMake(0,0,self.width,44.0f)]自动释放];[navigationBar pushNavigationItem:navigationItem动画:否];[self addSubview:navigationBar];}
我使用了标准的NavigationBar外观,但是您可以放入任何喜欢的东西,包括按钮,文本框,机器人独角兽的图像,作品
如果您没有获得上述代码中发生的所有事情,则可能需要重新学习委派"并以编程方式创建视图.
I have a UITableView with an Add cell... row and would like to have a keyboard pop up with a view above it like in the "Messages" application, where the user can type the name of the new cell.
I realize that there are several other ways to get user data, and several ways to implement the functionality that I am trying to achieve.
For example, the iPod application presents a popup when a new playlist should be created.
Right now, I have a hidden text field that is set to be the first responder when the Add cell... row is pressed, and I assign the view containing the input field and confirmation button as the inputAccessoryView for this hidden field. Alternatively, I could add this view as a subview of the table and position it using keyboard notifications.
I would just like to know if there is a cleaner way to accomplish what I am trying to do (such as setting the inputAccessoryView of the input textField to be displayed to be the textField's superview). I have tried several approaches, but I cannot seem to be able to dismiss the keyboard using resignFirstResponder when the view should close. I can get the inputAccessoryView to disappear, but the keyboard remains resolutely open, taking up necessary screen real estate. Also, when the nib's view is a UITableView with a UITableViewController as the File's Owner rather than a UIView with a UIViewController as the File's Owner, I get a very odd error: "setting the first responder view of the table but we don't know its type (cell/header/footer)"
Thanks in advance,Julian Ceipek
You are on the right track with the setInputAccessoryView
on the UITextView/UITextField classes' This method allows you to add any view you want to the top of a keyboard.The view you create would then use a delegation method to tell the main view controller to resignFirstResponder.
So, to get you started:
@protocol TextFieldAccessoryViewDelegate
@required
- (void)keyboardShouldDismiss;
@end
@interface TextFieldAccessoryView : UIView
@property (nonatomic, retain) id<TextFieldAccessoryViewDelegate> delegate;
- (id)initWithFrame:(CGRect)frame withDelegate (id<TextFieldAccessoryViewDelegate>)aDelegate;
@end
The implementation might look a little like (only posting the code that makes the view):
#pragma mark - Private methods
- (void)doneButtonTapped:(id)sender
{
[delegate keyboardShouldDismiss];
}
- (void)setUpChildrenView
{
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(doneButtonTapped:)];
UINavigationItem *navigationItem = [[UINavigationItem alloc] initWithTitle:@""];
[navigationItem setRightBarButtonItem:doneButton];
[navigationItem setHidesBackButton:YES];
UINavigationBar *navigationBar = [[[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.width, 44.0f)] autorelease];
[navigationBar pushNavigationItem:navigationItem animated:NO];
[self addSubview:navigationBar];
}
I have used a standard NavigationBar looking view, but you could put in anything you like and include buttons, textfields, images of robot unicorns, the works
If you don't get everything thats going on in the above code you might need to brush up on Delegation and creating views programmatically.
这篇关于将UITextField的inputAccessoryView设置为其超级视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!