使不可响应事件的UILabel响应事件

使不可响应事件的UILabel响应事件


  • 说明:响应的结果为弹出对话框通知

1. 新建一个继承自UILabel的类,暂且为EventableLabel

2. 在EventableLabel.m中,需要添加的方法有


  • - (BOOL)canBecomeFirstResponder此为重写

  • - (id)initWithFrame:(CGRect)frame此为重写

  • - (void)awakeFromNib此为重写

  • - (void)attachTapEvent

  • - (void)handleTap:(UITapGestureRecognizer *)recognizer

3. 上代码


  • EventableLabel.h,这里面没什么东西

     #import 
    @interface EventableLabel : UILabel
    @end

  • EventableLabel.m


    • 使其label可以成为第一响应者

    • 不过经过实践,好像没有这个也能实现

     //make the label be able to receive event

    • (BOOL)canBecomeFirstResponder
      {
      return YES;
      }


  • 执行某种方法将事件绑定到绑定上

  • initWithFrame是在控件初始化的时候绑定

  • awakeFromNib是在控件。。。的时候绑定

 //bind the event

  • (id)initWithFrame:(CGRect)frame
    {
    self = [super initWithFrame:frame];
    if (self){
    [self attachTapEvent];
    }

      NSLog(@"initWithFrame");
      return self;
    

}

  • (void)awakeFromNib
    {
    [super awakeFromNib];
    [self attachTapEvent];

      NSLog(@"awakeFromNib");
    

}


  • 将事件绑定到控件的方法

 //add the tap event

  • (void)attachTapEvent
    {
    self.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTap:)];
    tapGesture.numberOfTapsRequired = 1;
    [self addGestureRecognizer:tapGesture];

      NSLog(@"attachTapEvent");
    

}


  • 事件发生时执行的方法

  • 对于UITapGestureRecognizer来说,使用UIGestureRecognizerStateEnded来判别动作

 //how to deal with the tap event

  • (void)handleTap:(UITapGestureRecognizer *)recognizer
    {
    if(recognizer.state == UIGestureRecognizerStateEnded){
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Tap" message:@"Label Tap Event Detected..." delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];

      [alertView show];
      }
    
      NSLog(@"handleTap");
    

}


4. 运行代码


  • 发现什么也没有

  • 因为还没有创建EventableLabel的实例

5. 创建EventableLabel实例

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

CGRect rect = CGRectMake(50, 50, 100, 100);
EventableLabel *eventableLabel = [[EventableLabel alloc]initWithFrame:rect];
eventableLabel.text = @"I am here";

[self.view addSubview:eventableLabel];

}

6. 另


  • 上述代码中UIAlertView已经在iOS8中所deprecated,取而代之为UIAlertController

  • 下面将UIAlertView部分的代码替换为UIalertController


    • 创建一个UIAlertController

       UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Tap" message:@"Tap Gesture Detected..." preferredStyle:UIAlertControllerStyleAlert];

    • alert添加上按钮

       UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){
      self.backgroundColor = [UIColor whiteColor];
      }];
      [alert addAction:alertAction];

    • 显示出alert

       UIViewController *viewController = [self getViewController];
      [viewController presentViewController:alert animated:YES completion:nil];

    • 问题来了,显示alert部分的代码为什么如此突兀?



  • 由于presentViewController:animated:completion:这条消息需要一个UIViewController来发出;然而本类却是一个继承自UILabel的普通控件而已,因此需要寻找一个UIViewController,而刚好,EventableLabel的下一个响应者是ViewController,于是有下列函数,参考自http://hufeng825.github.io/2013/08/29/ios5/

     - (UIViewController *)getViewController
    {
    id object = self;
    while (![object isKindOfClass:[ViewController class]]) {
    object = [object nextResponder];
    }
    return object;
    }

  • 上面代码不断寻找一个ViewController的类,找到便返回

以上

你可能感兴趣的:(使不可响应事件的UILabel响应事件)