更改MFMailComposeViewController的标题

时间:2009-11-15 15:34:14

标签: iphone objective-c email iphone-sdk-3.0 mfmailcomposeviewcontroller

我在我的应用中使用MFMailComposeViewController进行应用内电子邮件,但我无法更改标题。默认情况下,它会在标题中显示主题,但我想将标题设置为其他内容。我怎么能这样做?

我试过了:

controller.title = @"Feedback";

但它不起作用。

这是我的代码:

- (IBAction)email {
    NSArray *array = [[NSArray alloc] initWithObjects:@"myemail@gmail.com", nil];
    MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
    [[controller navigationBar] setTintColor:[UIColor colorWithRed:0.36 green:0.09 blue:0.39 alpha:1.00]];
    controller.mailComposeDelegate = self;
    controller.title = @"Feedback";
    [controller setSubject:@"Long subject"];
    [controller setMessageBody:@""
                        isHTML:NO];
    [controller setToRecipients:array];
    [self presentModalViewController:controller animated:YES];
    [controller release];
    [array release];
}

- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
    [self becomeFirstResponder];
    [self dismissModalViewControllerAnimated:YES];
}

6 个答案:

答案 0 :(得分:21)

您可以使用单行为MFMailComposeViewController设置不同的标题,如此。

...
[self presentModalViewController:controller animated:YES]; // Existing line
[[[[controller viewControllers] lastObject] navigationItem] setTitle:@"SomethingElse"];
...

但是,此实现有效地依赖于MFMailComposeViewController的未记录的功能。您正在访问私有类的navigationItem(_MFMailComposeRootViewController)并将其标题更改为邮件主题以外的其他内容。我回应Art Gillespie的观点,因为你不应该这样做并且很可能被Apple评论家拒绝做这样的事情。此外,此过程可能会在iPhone OS的任何次要版本中完全更改,可能会导致您的用户崩溃,直到您可以发布更新来修复该行为。

然而,决定取决于你,如果你仍想采取这些未经推荐的步骤,那就是你的做法。

答案 1 :(得分:14)

来自MFMailComposeViewController类参考:

  

重要提示:邮件撰写界面本身不可自定义,您的应用程序不得修改。此外,在显示界面后,您的应用程序不允许对电子邮件内容进行进一步更改

答案 2 :(得分:3)

似乎消息的主题在iOS 8中提供了MFMailComposeViewController的标题。

答案 3 :(得分:2)

你应该只能看一个视图(controller.view)并把它放在你的控制器里......在那一刻,你没有修改任何东西,你实际上在iPad上做的几乎像苹果一样撰写电子邮件时的电子邮件应用程序......同样的事情也适用于iPhone ......

答案 4 :(得分:2)

Sbrocket的答案很有效。这是添加标题视图(标签)的方法:

// existing
[self presentModalViewController:controller animated:YES];

// new code
CGRect frame = CGRectMake(0, 0, 320, 44);
UILabel *label = [[UILabel alloc] initWithFrame:frame];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont fontWithName:@"MarkerFelt-Thin" size:18.0];
label.adjustsFontSizeToFitWidth = YES;
label.minimumFontSize = 12.0;
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor darkGrayColor];
label.text = @"Your Comments";
[[[[controller viewControllers] lastObject] navigationItem] setTitleView:label];

与上述相同的评论,并不是真的建议自定义MFMailComposeViewController ...

答案 5 :(得分:0)

最有可能的是,您必须在视图层次结构中挖掘以查找包含标题的原始UINavigationBar,并在其上手动设置标题。

程序类转储在这里可以派上用场来确定使用的确切类。试用,错误和调试器很可能是您最好的选择。