UILabel不显示inputView [英] UILabel doesn't show inputView

查看:122
本文介绍了UILabel不显示inputView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会使用UILabel来允许用户使用UIDatePicker选择日期。

I would use a UILabel to allow users to select a date with UIDatePicker.

为此,我创建了一个UILabel子类,覆盖了inputView和inputAccessoryView属性他们可写;我还实现了 - (BOOL)canBecomeFirstResponder和 - (BOOL)isUserInteractionEnabled方法,两者都返回YES。
然后我将一个UIDatePIcker实例分配给inputView属性。

To do this, I created an UILabel subclass overwriting the inputView and the inputAccessoryView properties making them writable; I also implemented the -(BOOL) canBecomeFirstResponder and the -(BOOL) isUserInteractionEnabled methods returning YES for both. Then I assigned an instance of UIDatePIcker to the inputView property.

此时我的期望是,当点击标签时,应该出现UIDatePicker,但没有发生。

At this point my expectation is that when the label is tapped an UIDatePicker should appear, but nothing happens.

任何帮助?

这是代码:

YPInteractiveUILabel.h

@interface YPInteractiveUILabel : UILabel
    @property (readwrite) UIView *inputView;
    @property (readwrite) UIView *inputAccessoryView;

- (BOOL) canBecomeFirstResponder;
- (BOOL) isUserInteractionEnabled;
@end


YPInteractiveUILabel.h

#import "YPInteractiveUILabel.h"

@implementation YPInteractiveUILabel

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];

    if (self)
    {
        UIDatePicker *datePicker = [[UIDatePicker alloc] init];
        [self  setInputView:datePicker];
    }

    return self;
}

- (BOOL)isUserInteractionEnabled
{
    return YES;
}

- (BOOL)canBecomeFirstResponder
{
    return YES;
}

@end


推荐答案

感谢这些建议(特别是NeverBe的评论和rdelmar提出的答案)我在代码中发现了问题。
简而言之,为了显示输入标签,需要在用户点击标签时调用becomeFirstResponder方法。

Thanks to the suggestions (especially the comment from NeverBe and the answer proposed by rdelmar) I found the problem in my code. In brief, in order to show the input label, a call to the becomeFirstResponder method when the user tap the label is needed.

遵循UILabel子类实现更正(头文件保持不变):

Following the UILabel subclass implementation corrected (the header file remains the same):

@implementation YPInteractiveUILabel

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];

    if (self)
    {
        UIDatePicker *datePicker = [[UIDatePicker alloc] init];
        [self  setInputView:datePicker];

        UITapGestureRecognizer *tapper = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(launchPicker:)];
        [self addGestureRecognizer:tapper];

    }

    return self;
}

- (BOOL)isUserInteractionEnabled
{
    return YES;
}

- (BOOL)canBecomeFirstResponder
{
    return YES;
}

-(void)launchPicker:(UITapGestureRecognizer *) tapper
{
    [self becomeFirstResponder];
}


@end

这篇关于UILabel不显示inputView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆