<h2>使不可響應(yīng)事件的<code>UILabel</code>響應(yīng)事件</h2>
<ul>
<li>說明:響應(yīng)的結(jié)果為彈出對話框通知</li>
</ul>
<h3>1. 新建一個(gè)繼承自<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>可以成為第一響應(yīng)者</li>
<li>不過經(jīng)過實(shí)踐荞雏,好像沒有這個(gè)也能實(shí)現(xiàn)</li>
</ul>
<pre><code> //make the label be able to receive event
- (BOOL)canBecomeFirstResponder
{
return YES;
}
</code></pre>
<ul>
<li>執(zhí)行某種方法將事件綁定到綁定上</li>
<li><code>initWithFrame</code>是在控件初始化的時(shí)候綁定</li>
<li><code>awakeFromNib</code>是在控件况毅。胶征。。的時(shí)候綁定</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>事件發(fā)生時(shí)執(zhí)行的方法</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. 運(yùn)行代碼</h3>
<ul>
<li>發(fā)現(xiàn)什么也沒有</li>
<li>因?yàn)檫€沒有創(chuàng)建<code>EventableLabel</code>的實(shí)例</li>
</ul>
<h3>5. 創(chuàng)建<code>EventableLabel</code>實(shí)例</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>已經(jīng)在iOS8中所deprecated陌宿,取而代之為<code>UIAlertController</code></li>
<li>下面將<code>UIAlertView</code>部分的代碼替換為<code>UIalertController</code>
<ul>
<li><p>創(chuàng)建一個(gè)<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>這條消息需要一個(gè)<code>UIViewController</code>來發(fā)出;然而本類卻是一個(gè)繼承自<code>UILabel</code>的普通控件而已舶得,因此需要尋找一個(gè)<code>UIViewController</code>掰烟,而剛好,<code>EventableLabel</code>的下一個(gè)響應(yīng)者是<code>ViewController</code>沐批,于是有下列函數(shù)纫骑,參考自<a >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>上面代碼不斷尋找一個(gè)<code>ViewController</code>的類,找到便返回</li>
</ul>
<h1>以上</h1>