簡體   English   中英

UILabel上的手勢識別器不起作用

[英]Gesture recogniser on UILabel, not working

我試圖在UILabel上應用UITapGestureRecognizer以便檢查並打開電子郵件服務。 當前的UIViewUIViewController的一部分,並在用戶點擊按鈕后顯示。

#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 ,也無法點擊它,並且電子郵件功能也從未觸發過。

很高興您知道了。 是的,如果一個UI元素小於其所有超級視圖(一直到整個鏈),則所有與該元素的UI交互都將被阻止。 這適用於標簽,按鈕等。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM