<h2>使不可响应事件的<code>UILabel</code>响应事件</h2>
<ul>
<li>说明:响应的结果为弹出对话框通知</li>
</ul>
<h3>1. 新建一个继承自<code>UILabel</code>的类,暂且为<code>EventableLabel</code></h3>
<h3>2. 在<code>EventableLabel.m</code>中,需要添加的方法有</h3>
<ul>
<li><code>- (BOOL)canBecomeFirstResponder</code>此为重写</li>
<li><code>- (id)initWithFrame:(CGRect)frame</code>此为重写</li>
<li><code>- (void)awakeFromNib</code>此为重写</li>
<li><code>- (void)attachTapEvent</code></li>
<li><code>- (void)handleTap:(UITapGestureRecognizer *)recognizer</code></li>
</ul>
<h3>3. 上代码</h3>
<ul>
<li><p><code>EventableLabel.h</code>,这里面没什么东西</p>
<pre><code> #import <UIKit/UIKit.h>
@interface EventableLabel : UILabel
@end
</code></pre></li>
<li><code>EventableLabel.m</code>
<ul>
<li>使其<code>label</code>可以成为第一响应者</li>
<li>不过经过实践,好像没有这个也能实现</li>
</ul>
<pre><code> //make the label be able to receive event
- (BOOL)canBecomeFirstResponder
{
return YES;
}
</code></pre>
<ul>
<li>执行某种方法将事件绑定到绑定上</li>
<li><code>initWithFrame</code>是在控件初始化的时候绑定</li>
<li><code>awakeFromNib</code>是在控件。。。的时候绑定</li>
</ul>
<pre><code> //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");
}
</code></pre>
<ul>
<li>将事件绑定到控件的方法</li>
</ul>
<pre><code> //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");
}
</code></pre>
<ul>
<li>事件发生时执行的方法</li>
<li>对于<code>UITapGestureRecognizer</code>来说,使用<code>UIGestureRecognizerStateEnded</code>来判别动作</li>
</ul>
<pre><code> //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");
}
</code></pre></li>
</ul>
<h3>4. 运行代码</h3>
<ul>
<li>发现什么也没有</li>
<li>因为还没有创建<code>EventableLabel</code>的实例</li>
</ul>
<h3>5. 创建<code>EventableLabel</code>实例</h3>
<pre><code>- (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];
}
</code></pre>
<h3>6. 另</h3>
<ul>
<li>上述代码中<code>UIAlertView</code>已经在iOS8中所deprecated,取而代之为<code>UIAlertController</code></li>
<li>下面将<code>UIAlertView</code>部分的代码替换为<code>UIalertController</code>
<ul>
<li><p>创建一个<code>UIAlertController</code></p>
<pre><code> UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Tap" message:@"Tap Gesture Detected..." preferredStyle:UIAlertControllerStyleAlert];
</code></pre></li>
<li><p>为<code>alert</code>添加上按钮</p>
<pre><code> UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){
self.backgroundColor = [UIColor whiteColor];
}];
[alert addAction:alertAction];
</code></pre></li>
<li><p>显示出<code>alert</code></p>
<pre><code> UIViewController *viewController = [self getViewController];
[viewController presentViewController:alert animated:YES completion:nil];
</code></pre></li>
<li>问题来了,显示<code>alert</code>部分的代码为什么如此突兀?</li>
</ul>
</li>
<li><p>由于<code>presentViewController:animated:completion:</code>这条消息需要一个<code>UIViewController</code>来发出;然而本类却是一个继承自<code>UILabel</code>的普通控件而已,因此需要寻找一个<code>UIViewController</code>,而刚好,<code>EventableLabel</code>的下一个响应者是<code>ViewController</code>,于是有下列函数,参考自<a href="http://hufeng825.github.io/2013/08/29/ios5/">http://hufeng825.github.io/2013/08/29/ios5/</a></p>
<pre><code> - (UIViewController *)getViewController
{
id object = self;
while (![object isKindOfClass:[ViewController class]]) {
object = [object nextResponder];
}
return object;
}
</code></pre></li>
<li>上面代码不断寻找一个<code>ViewController</code>的类,找到便返回</li>
</ul>
<h1>以上</h1>