我们在使用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. 【ABAP系列】SAP ABAP基础-abap数据类型的解析整理

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP基础-abap数 ...

  2. 【NOIP2017】列队【可持久化线段树】

    题目链接 题目描述 Sylvia 是一个热爱学习的女孩子. 前段时间,Sylvia 参加了学校的军训.众所周知,军训的时候需要站方阵. Sylvia 所在的方阵中有n×mn×m名学生,方阵的行数为 n ...

  3. GB/T2659-2000《世界各国和地区名称代码》

    代码 代码名称 004 阿富汗 008 阿尔巴尼亚 012 阿尔及利亚 016 美属萨摩亚 020 安道尔 024 安哥拉 660 安圭拉 010 南极洲 028 安提瓜和巴布达 032 阿根廷 05 ...

  4. jmeter 把返回数据写到文件

    jmeter如何把返回数据写入到文件 作者:WhoisTester    2015-10-20 20:11 1. 首先我们可以使用 regular expression extractor 正则表达式 ...

  5. vs code配置C/C++开发环境

    第一步:下载 Vs Code 点击链接下载Vs Code 下载版本  并安装 https://code.visualstudio.com/ 点击 Download for Windwos 安装时  如 ...

  6. css画百分比圆环

    html: <div class="circle"> <div class="percent-circle percent-circle-left&qu ...

  7. B bearBaby loves sleeping

    链接:https://ac.nowcoder.com/acm/contest/338/B来源:牛客网 题目描述 Sleeping is a favorite of little bearBaby, b ...

  8. ubuntu安装supervisor以及使用supervisor启动.net core进程

    1.下载.net core项目ubuntu系统运行容器dotnet      1.版本:dotnet-sdk-2.1.3-linux-x64.tar.gz      2.将下载好的包上传到ubuntu ...

  9. opencv保存图片路径包含中文乱码解决方案

    # coding: utf-8 import numpy as np import cv2 img = cv2.imread('1.jpg',1) cv2.imshow('image', img) k ...

  10. Cheatsheet: 2019 07.01 ~ 09.30

    Other Intro Guide to Dockerfile Best Practices QuickJS Javascript Engine Questions for a new technol ...