IOS学习笔记(六)inputAccessoryView,inputView
来源:互联网 发布:nba关注软件 编辑:程序博客网 时间:2024/04/29 15:07
我们在使用UITextView和UITextField的时候,可以通过它们的inputAccessoryView属性给输入时呼出的键盘加一个附属视图,通常是UIToolBar,用于回收键盘。
但是当我们要操作的视图不是UITextView或UITextField的时候,inputAccessoryView就变成了readonly的。
这时我们如果还想再加inputAccessoryView,按API中的说法,就需要新建一个该视图的子类,并重新声明inputAccessoryView属性为readwrite的。比如我们要实现点击一个tableView的一行时,呼出一个UIPickerView,并且附加一个用于回收PickerView的toolbar。因此我们自建一个UITableViewCell类,并声明inputAccessoryView和inputView为readwrite的,并且重写它们的get方法,这样在某个tableviewcell变成第一响应者时,它就会自动呼出inputView和inputAccessoryView;
1 @interface MyTableViewCell : UITableViewCell<UIPickerViewDelegate,UIPickerViewDataSource>2 {3 UIToolbar *_inputAccessoryView;4 UIPickerView *_inputView;5 }6 @property(strong,nonatomic,readwrite) UIToolbar *inputAccessoryView;7 @property(strong,nonatomic,readwrite) UIPickerView *inputView;8 @end
.m中的get方法:
1 -(UIToolbar *)inputAccessoryView 2 { 3 if(!_inputAccessoryView) 4 { 5 UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)]; 6 // UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonItem target:self action:@selector(dodo)]; 7 UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dodo)]; 8 toolBar.items = [NSArray arrayWithObject:right]; 9 return toolBar;10 }11 return _inputAccessoryView;12 }13 -(UIPickerView *)inputView14 {15 if(!_inputView)16 {17 UIPickerView * pickView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 200, 320, 200)];18 pickView.delegate =self;19 pickView.dataSource = self;20 pickView.showsSelectionIndicator = YES;21 return pickView;22 }23 return _inputView;24 }25 -(void)dodo26 {27 [self resignFirstResponder];28 }29 - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView30 {31 return 1;32 }33 - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component34 {35 return [NSString stringWithFormat:@"%d",row];36 }37 // returns the # of rows in each component..38 - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component39 {40 return 5;41 }
但是这时运行后还是没有反应,最后在一个网页中查到这样的话:
What is the best, we aren't limited to use this feature on UITextFields only. Because of fact that UIView inherits from UIResponder, we can attach this behaviour to all views, for example to a button or a table cell. To do that we have to override canBecomeFirstResponder
method in our subclass. For example, the UIButton subclass implementation can look like this:
implementation CustomButton { } //it is UIButton subclass @synthesize inputView, inputAccessoryView; - (BOOL) canBecomeFirstResponder { return YES; } - (void)dealloc { [inputView release]; [inputAccessoryView release]; [super dealloc]; } @end因此我在.m中重写
canBecomeFirstResponder方法
-(BOOL)canBecomeFirstResponder{ return YES;}
但是这时运行是还是没有反应,最后我只好在代码中当cell被选中时,手动把它变成第一响应者。(难道cell被选中时不是第一响应者?)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; [cell becomeFirstResponder];}
运行结果:
- IOS学习笔记(六)inputAccessoryView,inputView
- IOS学习笔记(六)inputAccessoryView,inputView
- IOS学习笔记(六)inputAccessoryView,inputView
- iOS inputView 和 inputAccessoryView
- ios开发-inputView和inputAccessoryView
- iOS开发inputView和inputAccessoryView
- inputAccessoryView,inputView
- inputAccessoryView,inputView
- iOS UITextField InputAccessoryView & InputView的使用方法
- UIResponder inputView与inputAccessoryView
- UIButton中的inputAccessoryView,inputView
- InputAccessoryView 与 inputView理解
- inputView与inputAccessoryView
- keyboard监听、inputView、inputAccessoryView
- inputAccessoryView和inputView的区别
- cell 中textFile 的inputView inputAccessoryView
- 【iOS开发-70】点菜系统案例:利用文本框的inputView和inputAccessoryView串联UIPickerView、UIDatePicker和UIToolBar
- ios学习笔记(六)视图控制器
- Android学习笔记六十二:Java并发编程:volatile关键字解析
- Lecture 5:把无限多个假设降到有限个
- 快速排序详解
- Git开发笔记——搭建Git简易高效服务器
- sqlserver 数据库用户密码过期
- IOS学习笔记(六)inputAccessoryView,inputView
- leetcode-Java-15. 3Sum
- mysql的审计功能
- 浅说css的绝对定位跟相对定位
- Hadoop之——错误及解决方案总结
- Java内存模式以及回收模式
- ORA-00257 archiver error. 错误的处理方法
- Java基础--定时任务Timer
- 随机梯度下降与梯度下降讲解