0

我在其中一个视图控制器中使用 UITabbarcontroller,在一个标签栏中我只需要有邮件控制器,当我这样做时,就会进入无限循环,我将如何克服它,代码相关

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
    mailViewController.mailComposeDelegate = self;

    if ([MFMailComposeViewController canSendMail]) {

        [mailViewController setToRecipients:[NSArray arrayWithObject:@"k.sourish.k@gmail.com"]];
        [mailViewController setSubject:@"Subject Goes Here."];
        [mailViewController setMessageBody:@"Your message goes here." isHTML:NO];

        [self presentModalViewController:mailViewController animated:YES];
    }
    else {

        NSLog(@"Device is unable to send email in its current state.");
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Error" message:@" Please Configure Your Mail Account" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
        [alert release];

    }
    [mailViewController release];
}

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {    NSString *message = @"";
    // Notifies users about errors associated with the interface
    switch (result) {
        case MFMailComposeResultCancelled:
            message = @"Mail: canceled";
            break;
        case MFMailComposeResultSaved:
            message = @"Mail: saved";
            break;
        case MFMailComposeResultSent:
            message = @"Mail: sent";
            break;
        case MFMailComposeResultFailed:
            message = @"Mail: failed";
            break;
        default:
            message = @"Mail: not sent";
            break;
    }
    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Alert" message:message delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alert show];
    [alert release];

    [controller dismissModalViewControllerAnimated:YES];
}
4

1 回答 1

1

我会这样做

在您的 UITabBar 上拖放一个 UITabBarButton 对象并将其命名为“电子邮件”。现在创建这个 IBAction

-(IBAction)composeMyEmail
{

    MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
        mailViewController.mailComposeDelegate = self;

        if ([MFMailComposeViewController canSendMail]) {

            [mailViewController setToRecipients:[NSArray arrayWithObject:@"k.sourish.k@gmail.com"]];
            [mailViewController setSubject:@"Subject Goes Here."];
            [mailViewController setMessageBody:@"Your message goes here." isHTML:NO];

            [self presentModalViewController:mailViewController animated:YES];
        }
        else {

            NSLog(@"Device is unable to send email in its current state.");
            UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Error" message:@" Please Configure Your Mail Account" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];
            [alert release];

        }
        [mailViewController release];

    }

将您的 IBAction 连接到您的“电子邮件”按钮。

删除 viewWillAppear 下的所有代码

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}
于 2013-06-23T16:39:55.880 回答