2014-12-04 68 views
0

當我單擊UIActionSheet中的電子郵件選項時,如何顯示郵件編輯器,我有點困惑。 這裏是我的示例代碼:如何在UIActionSheet中點擊索引按鈕時撰寫郵件?

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { 
    if(buttonIndex == 0) 
    { 
     NSString *emailTitle = @"Test Email"; 
     NSString *messageBody = @"iOS programming is so fun!"; 
     NSArray *toRecipents = [NSArray arrayWithObject:@"[email protected]"]; 
     MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init]; 
     mc.mailComposeDelegate = self; 
     [mc setSubject:emailTitle]; 
     [mc setMessageBody:messageBody isHTML:NO]; 
     [mc setToRecipients:toRecipents]; 
     [self presentViewController:mc animated:YES completion:NULL]; 
    } 
} 
+1

你有什麼問題

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error { //Add an alert in case of failure [self dismissViewControllerAnimated:YES completion:nil]; } 
在actionsheet

? – 2014-12-04 11:54:29

+0

UIViewController是否嵌入在UINavigationController中? – Anil 2014-12-04 11:56:10

回答

1

確保您添加到您的.h文件,並導入MessageUI.framework

這裏是你尋找什麼。我經常使用它。通過UIActionSheets在iOS版8.這裏不推薦的方式你走,但:

.H

ViewController : UIViewController <MFMailComposeViewControllerDelegate> 

.M

- (IBAction)showActionSheet:(id)sender { 

UIActionSheet *moreActions = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Add To Favorites", @"Search",@"Email", nil]; 
[moreActions showInView:self.view]; 
} 

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { 

switch (buttonIndex) { 
    case 0: [self addToFav:self]; 
     break; 
    case 1: ; [self popSearchBar:self]; 
     break; 
    case 2: { [self sendEmail]; 
     break; 
    } 
     break; 
} 
} 
- (void)sendEmail { 
NSString *emailTitle = @"This is email title"; 
// Email Content as for HTML 
NSString *messageBody = [NSString stringWithFormat:@"I may have found a missing document in your catalog. I tried opening :<p><strong><font color=\"red\"> %@ </font><br>'%@'</strong></p> with no results. Can I have a dollar for reporting this?", self.title, self.titleString]; 
// To address 
NSArray *toRecipents = [NSArray arrayWithObject:@"[email protected]"]; 

MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init]; 
mc.mailComposeDelegate = self; 
[mc setSubject:emailTitle]; 
[mc setMessageBody:messageBody isHTML:YES]; 
[mc setToRecipients:toRecipents]; 

// Present mail view controller on screen 
[self presentViewController:mc animated:YES completion:NULL]; 

} 
- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult: (MFMailComposeResult)result error:(NSError *)error 
{ 
switch (result) 
{ 
    case MFMailComposeResultCancelled: 
     NSLog(@"Mail cancelled"); 
     [self dismissViewControllerAnimated:YES completion:NULL]; 
     break; 
    case MFMailComposeResultSaved: 
     NSLog(@"Mail saved"); 
     [self dismissViewControllerAnimated:YES completion:NULL]; 
     break; 
    case MFMailComposeResultSent: 
     NSLog(@"Mail sent"); 
     [self dismissViewControllerAnimated:YES completion:NULL]; 
     break; 
    case MFMailComposeResultFailed: 
     NSLog(@"Mail sent failure: %@", [error localizedDescription]); 
     break; 
    default: 
     break; 
} 
// Close the Mail Interface 
[self dismissViewControllerAnimated:YES completion:NULL]; 
} 

還要注意,有些人的遊戲模擬器電子郵件的問題。在放棄之前在設備上試用它。

+0

Thanx爲你的時間,但實際上是什麼果醬面臨是我有uiactionsheet顯示兩個選項稱爲電子郵件,Gmail。當我點擊電子郵件選項的行動表中,我需要從收件人和收件人的電子郵件頁面 - – 2014-12-04 12:37:38

+0

我不明白你在問什麼@Anandios你可以更具體 – soulshined 2014-12-04 12:40:21

0

這是子選項的代碼。而不是索引使用按鈕的名稱來激發你的郵件選項。

1

下面的代碼:

首先添加和導入信息框架:

#import <MessageUI/MessageUI.h> 

然後標記你的自我作爲這樣

@interface MYViewController() <MFMailComposeViewControllerDelegate> 

然後委託拉起作曲:

- (IBAction)emailButtonPressed:(id)sender 
{ 
    if ([MFMailComposeViewController canSendMail]) { 
     MFMailComposeViewController *composeViewController = [[MFMailComposeViewController alloc] initWithNibName:nil bundle:nil]; 
     [composeViewController setMailComposeDelegate:self]; 
     [composeViewController setToRecipients:@[@"[email protected]"]]; 
     [composeViewController setSubject:@"example subject"]; 
     [self presentViewController:composeViewController animated:YES completion:nil]; 
    } 
} 

然後辦理委託回調和罷免作曲家:使用actionSheet: clickedButtonAtIndex:

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { 

    //Get the name of the current pressed button 
    NSString *buttonTitle = [actionSheet buttonTitleAtIndex:buttonIndex]; 

    if ([buttonTitle isEqualToString:@"Your button title"]) { 
     [self btnContact]; 
    } 

} 

調用此方法

- (void)btnContact{ 
    // Email Subject 
    NSString *emailTitle = @""; 
    // Email Content 
    NSString *messageBody = @""; 
    // To address 
    NSString *toRecp = @""; 
    NSArray *toRecipents = [NSArray arrayWithObject:toRecp]; 

    MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init]; 
    mc.mailComposeDelegate = self; 
    [mc setSubject:emailTitle]; 
    [mc setMessageBody:messageBody isHTML:NO]; 
    [mc setToRecipients:toRecipents]; 

    // Present mail view controller on screen 
    [self presentViewController:mc animated:YES completion:NULL]; 
} 
+0

Thanx爲你的時間,但實際上是什麼果醬面臨的是我有uiactionsheet顯示兩個選項稱爲電子郵件,Gmail。當我點擊的動作片電子郵件選項我需要得到電子郵件頁面與和收件人 – 2014-12-04 12:36:46

+0

@Anandios看到我的更新答案.. – 2014-12-04 12:45:48

+0

我得到的日誌這樣的響應:2014年12月4日18:41:58.020 AmslideMenu [5968: 607]警告:嘗試在上呈現,其視圖不在窗口層次結構中! \ – 2014-12-04 13:14:08

相關問題