当点击并打开空白电子邮件时,我如何将UILabel作为按钮?

时间:2013-10-27 22:02:29

标签: objective-c

我已经构建了一个排序目录。有133人。我有三个标签。 “姓名”,“职位”和“电子邮件”。如何将电子邮件标签作为按钮打开带有标签地址的空白电子邮件?我遇到的问题是标签是动态的,我在设置它时遇到了麻烦。我是一个初学者,所以任何帮助将不胜感激!

这是.h文件 -

@interface PersonDetailControllerViewController : UIViewController

@property(weak) IBOutlet UILabel *name;
@property(weak) IBOutlet UILabel *position;
@property(weak) IBOutlet UILabel *email;

@property(strong) Person *person;

@end

这是.m文件 -

#import "PersonDetailControllerViewController.h"

@interface PersonDetailControllerViewController ()

@end

@implementation PersonDetailControllerViewController

@synthesize name, position, email, person;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];

self.name.text = self.person.name;
self.position.text = self.person.position;
self.email.text = self.person.email;

// Do any additional setup after loading the view.
}

@end

2 个答案:

答案 0 :(得分:2)

UIButton包含名为titleLabel的属性,实际上是UILabel,因此您无需使用UILabel,只需使用UIButton }}。你要做的是在添加按钮后,使用方法- (void)setTitle:(NSString *)title forState:(UIControlState)state。这会将titleLabel属性设置为您在传递它的NSString中指定的任何属性。

编辑:如果您已经开始使用UILabel,则可以执行以下操作:

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
label.text = @"My Label";
label.userInteractionEnabled = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(alert)];
[label addGestureRecognizer:tap];

然后在你的方法中,做你想做的任何事情。在我的情况下,alert被定义为:

- (void)alert
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Hello" message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alert show];
}

答案 1 :(得分:0)

好的,基本上您需要做的是创建另一个viewController,这将是一个通用页面,将根据选择的教师填充。当用户在tableView中点击一行时,您应该转换到detailView。有很多方法可以进行这种转换(程序,故事板segues等),但这里有一个例子:

TeacherDetailViewController *controller = [TeacherDetailViewController alloc] init];
controller.teacher = array[indexPath.row];

教师是新班级@property(nonatomic, strong) Person *person;的财产(TeacherDetailViewController)。通过设置教师属性,然后转换到该新类,新类将从所选的person对象中获取信息。

TeacherDetailViewController课程中,您可以设置包含电子邮件按钮的视图,在viewDidLoad中,您可以:

-(void)viewDidLoad {
    teacherName.text = self.person.name;

}

然后我会在该课程中有一个按钮,上面写着教师的电子邮件,该按钮应该链接到一个动作方法,它看起来像这样:

-(void)sendEmail {
 MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
mailViewController.mailComposeDelegate = self;
[mailViewController setToRecipients:self.teacher.email];

[mailViewController setSubject:@"Subject Goes Here."];
[mailViewController setMessageBody:@"Your message goes here." isHTML:NO];

[self presentModalViewController:mailViewController animated:YES];

}