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 对象表示一个可以接收触摸屏 ...
随机推荐
- Nginx 在各种语言框架下的配置 - 以 codeigniter 为例
对于各种语言常用的框架,Nginx 在官方的 Wiki 页面的 入门 部分提供了示例配置文件.具体可以参考这个页面的 Pre-canned Configurations 部分,这里列出了各种框架. 直 ...
- ICPC2019上海区域赛 部分题解(正在更新)
K. Color Graph 题意: 给定一个简单图,点个数<=16,删去部分边后,使得该图中无边数为奇数得环,问剩下的边数最大为多少? 思路: 如果一个图中无奇数边的环,那么这个图一定是个二分 ...
- Logistic Algorithm分类算法的Octave仿真
本次Octave仿真解决的问题是,根据两门入学考试的成绩来决定学生是否被录取,我们学习的训练集是包含100名学生成绩及其录取结果的数据,需要设计算法来学习该数据集,并且对新给出的学生成绩进行录取结果预 ...
- 基于Apache搭建HTTP HTTPS
参考资料 <openssl攻略>--第一章 <Apache服务器配置与使用工作笔记>-- 第六章 第十四章 https://juejin.im/post/5a31faf2518 ...
- Maven系列学习(一)Maven基本知识
Maven 简介 1.Maven主要是基于Java平台的项目构建,依赖管理和项目信息 2.Maven是优秀的构建工具,跨平台,消除构建的重复,抽象了一个完整的构建生命周期模型,标准化构建过程 3.管理 ...
- 图解Http阅读笔记(二)
简单的HTTP协议 HTTP是一种不保存状态,即无状态(stateless)协议.HTTP 协议自身不对请求和响应之间的通信状态进行保存.也就是说在 HTTP 这个级别,协议对于发送过的请求或响应都不 ...
- 洛谷P1265 公路修建——prim
给一手链接 https://www.luogu.com.cn/problem/P1265 这道题本质上就是最小生成树,题目描述就是prim的思想 TIP:注意稠密图和稀疏图的区别 #include&l ...
- 点分治题单(来自XZY)
点分治题单(来自XZY) 静态点分治 [x] 洛谷 P3806 [模板]点分治1 [x] 洛谷 P4178 Tree [x] 洛谷 P2634 [国家集训队]聪聪可可 [x] 洛谷 P4149 [IO ...
- 解决chrome浏览器安装不上的问题
1. 打开注册表: windows键 + R --> 输入regedit --> 回车 (注:windows键在左ctrl附近微软图标的键) 2. 找到 32位:HKEY_LOCA ...
- Linux学习笔记2-CentOS7安装tomcat8
1.下载tomcat:apache-tomcat-8.5.16.tar.gz 下载地址:http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat ...