博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
inputView与inputAccessoryView的使用,即自定义控件的响应视图
阅读量:2340 次
发布时间:2019-05-10

本文共 4292 字,大约阅读时间需要 14 分钟。

1、从功能上理解inputView与inputAccessoryView

inputView默认是显示键盘的view,如果重写这个view则不再弹出键盘,而是弹出自己的view.比如弹出一个日期选择器。inputView不会随着键盘出现而出现,设置了InputView只会当UITextField或者UITextView变为第一相应者时显示出来,不会显示键盘了。设置了InputAccessoryView,它会随着键盘一起出现并且会显示在键盘的顶端。InutAccessoryView默认为nil.

苹果说明如下:

// Presented when object becomes first responder.  If set to nil, reverts to following responder chain.  If set while first responder, will not take effect until reloadInputViews is called.
@property (readwrite, retain) UIView *inputView;             
@property (readwrite, retain) UIView *inputAccessoryView;

苹果官方对inputAccessoryView的说明如下:

UITextFields and UITextViews have an inputAccessoryView property, which you can set to any view, that is automatically displayed above and animated with the keyboard.

Note that the view you use should neither be in the view hierarchy elsewhere, nor should you add it to some superview, this is done for you.

翻译:
UITextFields和UITextView有一个inputAccessoryView的属性,当你想在键盘上展示一个自定义的view时,你就可以设置该属性。你设置的view就会自动和键盘keyboard一起显示了。

需要注意的是,你所自定义的view既不应该处在其他的视图层里,也不应该成为其他视图的子视图。其实也就是说,你所自定义的view只需要赋给属性inputAccessoryView就可以了,不要再做其他多余的操作。

2、举例如下(此例子参考博客网址为http://www.cnblogs.com/worldtraveler/archive/2012/09/18/2691406.html)

使用UITextView和UITextField的时,可以通过它们的inputAccessoryView属性给输入时呼出的键盘加一个附属视图,通常是UIToolBar,用于回收键盘。但是当我们要操作的视图不是UITextView或UITextField的时候,inputAccessoryView和inputView就变成了readonly的。这时我们如果想添加inputAccessoryView和inputView,按API中的说法,就需要新建一个该视图的子类,并重新声明inputAccessoryView和inputView属性为readwrite的,比如我们要实现点击一个tableView的一行时,呼出一个UIPickerView,并且附加一个用于回收PickerView的toolbar。因此我们自建一个UITableViewCell类,并声明inputAccessoryView和inputView为readwrite的,并且重写它们的get方法,这样在某个tableviewcell变成第一响应者时,它就会自动呼出inputView和inputAccessoryView;

h文件

@interface MyTableViewCell : UITableViewCell<UIPickerViewDelegate,UIPickerViewDataSource>
{
    UIToolbar *_inputAccessoryView;
    UIPickerView *_inputView;
}
@property(strong,nonatomic,readwrite) UIToolbar *inputAccessoryView;
@property(strong,nonatomic,readwrite) UIPickerView *inputView;
@end

m文件

-(UIToolbar *)inputAccessoryView

{
    if(!_inputAccessoryView)
    {
        UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
//        UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonItem target:self action:@selector(dodo)];
        UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dodo)];
        toolBar.items = [NSArray arrayWithObject:right];
        return toolBar;
    }
    return _inputAccessoryView;
}
-(UIPickerView *)inputView
{
    if(!_inputView)
    {
      UIPickerView *  pickView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 200, 320, 200)];
        pickView.delegate =self;
        pickView.dataSource = self;
        pickView.showsSelectionIndicator = YES;
        return pickView;
    }
    return _inputView;
}
-(void)dodo
{
    [self resignFirstResponder];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [NSString stringWithFormat:@"%d",row];
}
// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return 5;
}

但是这时运行后还是没有反应,最后在一个网页中查到这样的话:

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];}

运行结果:

你可能感兴趣的文章
RTSP协议分析(二)
查看>>
IPTV的前世今生与发展
查看>>
x264中的汇编x86inc.asm
查看>>
X264中的sad-a.asm
查看>>
x264中的cpu-a.asm
查看>>
x264中的DCT变换 dct-a.asm
查看>>
X264的时耗分析
查看>>
H.264 Profile、Level、Encoder三张简图
查看>>
NEON指令集综述
查看>>
FFmpeg的H.264解码器源代码简单分析:概述
查看>>
linux下编译调试x264
查看>>
debug和release版本的区别
查看>>
x86 指令集发展历程
查看>>
SLC、MLC、TLC闪存颗粒
查看>>
逐行Porgressive隔行Interlaced扫描的超详细讲解
查看>>
使用FFmpeg实现抠图合并功能(chroma key)
查看>>
长宽比 (视频)
查看>>
Pan & Scan和Letterbox
查看>>
资深影迷不可不知的宽高比:Aspect Ratio 电影画面比例
查看>>
MacBook Pro 外接显示器设置竖屏
查看>>