inputView与inputAccessoryView的使用,即自定义控件的响应视图

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

运行结果:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值