inputAccessoryView,inputView
我们在使用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 *)inputView
14 {
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)dodo
26 {
27 [self resignFirstResponder];
28 }
29 - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
30 {
31 return 1;
32 }
33 - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
34 {
35 return [NSString stringWithFormat:@"%d",row];
36 }
37 // returns the # of rows in each component..
38 - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
39 {
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];
}
运行结果:
inputAccessoryView,inputView的更多相关文章
- iOS开发小技巧--iOS键盘 inputView 和 inputAccessoryView
iOS键盘 inputView 和 inputAccessoryView 1.inputAccessoryView UITextFields和UITextViews有一个inputAccessoryV ...
- iOS开发inputView和inputAccessoryView
1.简介 起初看到这两个属性是在UIResponder中,只是可读的: @property (nullable, nonatomic, readonly, strong) __kindof UIVie ...
- (转发)InputAccessoryView的使用方法
转自:http://blog.sina.com.cn/s/blog_45e2b66c01015we9.html UITextFields and UITextViews have an inputAc ...
- 通用方法解决UITextFiled输入的时候,键盘遮挡问题
我们在用键盘录入的时候,有可能会遮挡录入框,所以我们应调整UIView的位置,使其不被遮挡.我写了一个通用的方法可以解决这个问题:1. [代码][C/C++]代码 - (void) ...
- IOS开发基础知识碎片-导航
1:IOS开发基础知识--碎片1 a:NSString与NSInteger的互换 b:Objective-c中集合里面不能存放基础类型,比如int string float等,只能把它们转化成对象才可 ...
- IOS开发基础知识--碎片17
1:contentSize.contentInset和contentOffset区别 contentSize 是scrollview中的一个属性,它代表scrollview中的可显示区域,假如有一个s ...
- 你真的了解UIResponder吗?
1:首先查看一下关于UIResponder的定义 NS_CLASS_AVAILABLE_IOS(2_0) @interface UIResponder : NSObject //响应链中负责传递事件的 ...
- iOS开发基础知识碎片
1:contentSize.contentInset和contentOffset区别 contentSize 是scrollview中的一个属性,它代表scrollview中的可显示区域,假如有一个s ...
- UIResponder
原网址:http://www.cnblogs.com/kuku/archive/2011/11/12/2246389.html 在 iOS 中,一个 UIResponder 对象表示一个可以接收触摸屏 ...
随机推荐
- 28. Jmeter函数
Jmeter函数传送门 软件测试汪简书地址 软件测试汪博客地址 欢迎关注微信公众号:软件测试汪.软件测试交流群:809111560 转载请注意出处,谢谢合作
- 2019/12/22 TZOJ
4986 Team Formation http://www.tzcoder.cn/acmhome/problemdetail.do?&method=showdetail&id=498 ...
- JavaScript DoublyLinkedList
function DoublyLinkedList() { var Node = function(element) { this.element = element; this.next = nul ...
- CentOS7通过SpeedTest工具测速
首先要安装SpeedTest工具,这里提供两种方法安装SpeedTest: 一.通过直接下载SpeedTest脚本,给权限运行脚本即可 [root@bogon ~]#wget -O speedtest ...
- SVN合并主干分支的方法
第一步 第二步 第三步 第四步
- Pandas的拼接操作
pandas的拼接操作 pandas的拼接分为两种: 级联:pd.concat, pd.append 合并:pd.merge, pd.join import pandas as pd import n ...
- wireshark自动化之tshark命令行
tshark是wireshark安装目录下命令行工具 使用tshark可以通过自动化方式调用wireshark tshark -a duration:30 抓包30秒-w cap.cap 保存为cap ...
- POJ 3764 The xor-longest Path (01字典树)
<题目链接> 题目大意: 给定一颗$n$个节点$(n\leq10^5)$,有边权的树,其边权$(0\leq w < 2^{31})$.让你求出这棵树上任意两个节点之间的异或最大值. ...
- 20180305-Python中迭代器和生成器
一.迭代器 迭代器是访问集合元素的一种方式.迭代器从访问到集合的第一个元素开始访问,直到所有元素被访问结束.而且迭代器只能往前访问,不能后退.另外迭代器的另一个优点,不会事先准备好访问的集合的所有元素 ...
- React(7) --react父子组件传参
react父子组件传参 父级向子级传参:在父组件中,我们引入子组件,通过给子组件添加属性,来起到传参的作用,子组件可以通过props获取父组件传过来的参数. 在父组件中: import React f ...