UILabel 上的手势识别器,不起作用 [英] Gesture recogniser on UILabel, not working
问题描述
我正在尝试在 UILabel
上应用 UITapGestureRecognizer
以检查和打开电子邮件服务.当前的 UIView
是 UIViewController
的一部分,并在用户点击按钮时显示.
I am trying to apply UITapGestureRecognizer
on a UILabel
in order to check and open e-mail service. The current UIView
is a part of a UIViewController
and displayed once user tap on a button.
#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>
@class AddressBook;
@interface ContactInfoUI : UIView <MFMailComposeViewControllerDelegate, UIGestureRecognizerDelegate>{
IBOutlet UIView *view;
UIViewController *mContainerVc;
AddressBook *mAddressBook;
}
@property (nonatomic, retain)UIView *view;
@property (nonatomic, retain)UIViewController *mContainerVc;
@property (nonatomic, retain)AddressBook *mAddressBook;
-(void)addContactInformationFrom:(AddressBook *)addressBook;
@end
.m
@implementation ContactInfoUI
@synthesize view;
@synthesize mContainerVc;
@synthesize mAddressBook;
- (id)init {
self = [super init];
if (self) {
[[NSBundle mainBundle] loadNibNamed:@"ContactInfoView" owner:self options:nil];
self.userInteractionEnabled = YES;
[self addSubview:[self view]];
}
return self;
}
-(void)addContactInformationFrom:(AddressBook *)addressBook{
self.mAddressBook = addressBook;
int y = 20;
CGRect rect = CGRectMake(20, y, 320, 60);
if (![mAddressBook.aEmail isEqualToString:@"-"]) {
UILabel *email = [[UILabel alloc] initWithFrame:rect];
UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showEmailForm:)];
tgr.delegate = self;
tgr.numberOfTapsRequired = 1;
[email addGestureRecognizer:tgr];
email.userInteractionEnabled = YES;
email.text = mAddressBook.aEmail;
[self addSubview:email];
rect.origin.y += 40;
}
}
-(IBAction)showEmailForm:(id)sender{
// Email Subject
NSString *emailTitle = @"Test Email";
// Email Content
NSString *messageBody = @"Some message";
// To address
NSArray *toRecipents = [NSArray arrayWithObject:@"test@apple.com"];
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc setSubject:emailTitle];
[mc setMessageBody:messageBody isHTML:NO];
[mc setToRecipients:toRecipents];
[mContainerVc presentViewController:mc animated:YES completion:NULL];
}
@end
VC.h(部分)
@class ContactInfoUI;
@class ElementObject;
@interface ElementDetailsViewController : UIViewController{
ElementObject *element;
IBOutlet ContactInfoUI *infoView;
}
@property(nonatomic, retain) ContactInfoUI *infoView;
- (IBAction)showInfo:(id)sender;
@end
VC.m(部分)
- (IBAction)showInfo:(id)sender {
if (infoView == nil) {
infoView = [[ContactInfoUI alloc] init];
infoView.userInteractionEnabled = YES;
infoView.mContainerVc = self;
}
AddressBook *ab = element.getElementAddressBook;
[infoView addContactInformationFrom:ab];
[self.view addSubview:infoView];
infoBtn.selected = YES;
sumBtn.selected = NO;
mapBtn.selected = NO;
infoView.hidden = NO;
staticMapScrView.hidden = YES;
summaryView.hidden = YES;
}
问题是,即使我可以在屏幕上看到实际的 UILabel
,我也无法点击它,电子邮件功能也从未被触发.
The problem is that, even if I can see the actual UILabel
on screen, I can't tap on it and the email function never fired.
推荐答案
很高兴你想到了.是的,如果一个 UI 元素小于它的任何一个 superviews(一直向上),所有与该元素的 UI 交互都将被阻止.这适用于标签、按钮等.
I'm glad you figured it. Yeah, if a UI element is smaller than any of the its superviews (all the way up the chain), all UI interaction to that element will be blocked. This goes for labels, buttons, etc..
这篇关于UILabel 上的手势识别器,不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!