我们在使用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 canBecomeFirstRespondermethod 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的更多相关文章

  1. iOS开发小技巧--iOS键盘 inputView 和 inputAccessoryView

    iOS键盘 inputView 和 inputAccessoryView 1.inputAccessoryView UITextFields和UITextViews有一个inputAccessoryV ...

  2. iOS开发inputView和inputAccessoryView

    1.简介 起初看到这两个属性是在UIResponder中,只是可读的: @property (nullable, nonatomic, readonly, strong) __kindof UIVie ...

  3. (转发)InputAccessoryView的使用方法

    转自:http://blog.sina.com.cn/s/blog_45e2b66c01015we9.html UITextFields and UITextViews have an inputAc ...

  4. 通用方法解决UITextFiled输入的时候,键盘遮挡问题

    我们在用键盘录入的时候,有可能会遮挡录入框,所以我们应调整UIView的位置,使其不被遮挡.我写了一个通用的方法可以解决这个问题:​​1. [代码][C/C++]代码         - (void) ...

  5. IOS开发基础知识碎片-导航

    1:IOS开发基础知识--碎片1 a:NSString与NSInteger的互换 b:Objective-c中集合里面不能存放基础类型,比如int string float等,只能把它们转化成对象才可 ...

  6. IOS开发基础知识--碎片17

    1:contentSize.contentInset和contentOffset区别 contentSize 是scrollview中的一个属性,它代表scrollview中的可显示区域,假如有一个s ...

  7. 你真的了解UIResponder吗?

    1:首先查看一下关于UIResponder的定义 NS_CLASS_AVAILABLE_IOS(2_0) @interface UIResponder : NSObject //响应链中负责传递事件的 ...

  8. iOS开发基础知识碎片

    1:contentSize.contentInset和contentOffset区别 contentSize 是scrollview中的一个属性,它代表scrollview中的可显示区域,假如有一个s ...

  9. UIResponder

    原网址:http://www.cnblogs.com/kuku/archive/2011/11/12/2246389.html 在 iOS 中,一个 UIResponder 对象表示一个可以接收触摸屏 ...

随机推荐

  1. Vue过滤器:全局过滤器

    Vue.js 允许你自定义过滤器,可被用于一些常见的文本格式化. 过滤器可以用在两个地方:双花括号插值和 v-bind 表达式 (后者从 2.1.0+ 开始支持). 过滤器应该被添加在 JavaScr ...

  2. Recurrent Neural Network(3):LSTM Basics and 《Inside Out》

    下图是Naive RNN的Recurrent Unit示意图,可以看到,在每个时间点t,Recurrent Unit会输出一个隐藏状态ht,对ht加工提取后将产生t时刻的输出yt.而在下一个时间节点t ...

  3. Run Your Tensorflow Deep Learning Models on Google AI

    People commonly tend to put much effort on hyperparameter tuning and training while using Tensoflow& ...

  4. 结合process进程,实现进程之间的通讯Queue,稍微复杂的运用

    #在父进程中创建两个子进程,一个往Queue写数据,一个从Queue里读数据 from multiprocessing import Queue,Process import time,random ...

  5. Hibernate使用时需要注意的几个小问题

    今天晚上玩了一下JDBC连接数据库,之后又利用Hibernate进行了数据库的访问,感觉利用Hibernate对数据库访问在文件配置好了之后确实更加简单快捷. 但是在操作的过程中也有一些细节需要注意一 ...

  6. P5445 [APIO2019]路灯(树套树)

    P5445 [APIO2019]路灯 转化为平面上的坐标(x,y),set维护连续区间. 用树套树维护矩阵加法,单点查询. 注意维护矩阵差分的时候, $(x,y,v)$是对$(x,y)(n+1,n+1 ...

  7. bzoj1779 [Usaco2010 Hol]Cowwar 奶牛战争(网络流)

    1779: [Usaco2010 Hol]Cowwar 奶牛战争 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 302  Solved: 131[Sub ...

  8. 获取input输入值

  9. Django中的APP

    3. Django中的APP: 什么是APP?以及为什么要用APP? project --> 项目 (老男孩教育大学校) APP --> 应用 (Linux学院/Python学院/大数据学 ...

  10. Django 配置 qq 邮箱发送邮件

    目录 一.实验环境 二.获取QQ邮箱授权码 1.什么是授权码? 2.怎么获取授权码? 三.Django中配置 setting.py中添加如下代码 文本邮件 HTML 邮件 一.实验环境 Python3 ...