0

這個問題是我難倒了,所以希望有人可以幫忙。我在一個視圖上有一個UIActionSheet,它有三個選項。一個將我的用戶帶到一個新視圖,一個通過電子郵件分享,另一個通過短信分享。UIActionSheet打開郵件應用程序iPhone

我已經創建了UIActionSheet這沒有問題的作品,在AlertSheet的新視圖部分也適用。我已經導入了Message.UI框架,並設置了郵件和短信收件人和作曲者,這很好。但是,我無法在UIActionSheet上設置兩個「按鈕」來打開郵件和短信。

通常我會通過界面生成器做到這一點,一個UIButton連接到我創建了行動,但因爲這是一個UIActionSheet不能那麼做。很抱歉,LONG代碼,但我覺得我需要顯示一切,所以請看下面;

-(IBAction)showActionSheet { 
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Choose an Option" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Application Support",@"Share Via Email",@"Share Via SMS",nil]; 
    [actionSheet showInView:self.view]; 
    [actionSheet release]; 
} 
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { 
    if(buttonIndex == 0) { 
    AppSupportView *controller = [[AppSupportView alloc] initWithNibName:@"AppSupportView" bundle:nil]; 
    controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; 
    [self presentModalViewController:controller animated:YES]; 
    [controller release]; 
    } 

    if(buttonIndex == 1) { 

    } 

    if(buttonIndex == 2) { 

    } 

} 

- (void)dealloc { 
    [feedbackMsg release]; 
    [super dealloc]; 
} 

- (void)viewDidUnload { 
    self.feedbackMsg = nil; 
} 

-(IBAction)showMailPicker:(id)sender { 
    // The MFMailComposeViewController class is only available in iPhone OS 3.0 or later. 
    // So, we must verify the existence of the above class and provide a workaround for devices running 
    // earlier versions of the iPhone OS. 
    // We display an email composition interface if MFMailComposeViewController exists and the device 
    // can send emails. Display feedback message, otherwise. 
    Class mailClass = (NSClassFromString(@"MFMailComposeViewController")); 

    if (mailClass != nil) { 
     //[self displayMailComposerSheet]; 
     // We must always check whether the current device is configured for sending emails 
     if ([mailClass canSendMail]) { 
      [self displayMailComposerSheet]; 
     } 
     else { 
      feedbackMsg.hidden = NO; 
      feedbackMsg.text = @"Device not configured to send mail."; 
     } 
    } 
    else { 
     feedbackMsg.hidden = NO; 
     feedbackMsg.text = @"Device not configured to send mail."; 
    } 
} 

-(IBAction)showSMSPicker:(id)sender { 
    // The MFMessageComposeViewController class is only available in iPhone OS 4.0 or later. 
    // So, we must verify the existence of the above class and log an error message for devices 
    //  running earlier versions of the iPhone OS. Set feedbackMsg if device doesn't support 
    //  MFMessageComposeViewController API. 
    Class messageClass = (NSClassFromString(@"MFMessageComposeViewController")); 

    if (messageClass != nil) {   
     // Check whether the current device is configured for sending SMS messages 
     if ([messageClass canSendText]) { 
      [self displaySMSComposerSheet]; 
     } 
     else { 
      feedbackMsg.hidden = NO; 
      feedbackMsg.text = @"Device not configured to send SMS."; 

     } 
    } 
    else { 
     feedbackMsg.hidden = NO; 
     feedbackMsg.text = @"Device not configured to send SMS."; 
    } 
} 

// Displays an email composition interface inside the application. Populates all the Mail fields. 
-(void)displayMailComposerSheet 
{ 
    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init]; 
    picker.mailComposeDelegate = self; 

    [picker setSubject:@"My BMR Index Rating from Total:Health App"]; 


    // Set up recipients 
    //NSArray *toRecipients = [NSArray arrayWithObject:@""]; 

    //[picker setToRecipients:toRecipients]; 
    NSString *emailSharing = @"I Just discovered that I have a Target Heart Rate of"; 
    // Fill out the email body text 
    [picker setMessageBody:emailSharing isHTML:YES]; 

    [self presentModalViewController:picker animated:YES]; 
    [picker release]; 
} 

// Displays an SMS composition interface inside the application. 
-(void)displaySMSComposerSheet 
{ 
    MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init]; 
    picker.messageComposeDelegate = self; 
    NSString *SMSShare = @"I Just discovered that I have a Target Heart Rate of"; 
    // Fill out the email body text 
    picker.body = SMSShare; 

    [self presentModalViewController:picker animated:YES]; 
    [picker release]; 
} 

// Dismisses the email composition interface when users tap Cancel or Send. Proceeds to update the 
// message field with the result of the operation. 
- (void)mailComposeController:(MFMailComposeViewController*)controller 
      didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error { 

    feedbackMsg.hidden = NO; 
    // Notifies users about errors associated with the interface 
    switch (result) 
    { 
     case MFMailComposeResultCancelled: 
      feedbackMsg.text = @"Result: Mail sending canceled"; 
      break; 
     case MFMailComposeResultSaved: 
      feedbackMsg.text = @"Result: Mail saved"; 
      break; 
     case MFMailComposeResultSent: 
      feedbackMsg.text = @"Result: Mail sent"; 
      break; 
     case MFMailComposeResultFailed: 
      feedbackMsg.text = @"Result: Mail sending failed"; 
      break; 
     default: 
      feedbackMsg.text = @"Result: Mail not sent"; 
      break; 
    } 
    [self dismissModalViewControllerAnimated:YES]; 
} 


// Dismisses the message composition interface when users tap Cancel or Send. Proceeds to update the 
// feedback message field with the result of the operation. 
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller 
       didFinishWithResult:(MessageComposeResult)result { 

    feedbackMsg.hidden = NO; 
    // Notifies users about errors associated with the interface 
    switch (result) 
    { 
     case MessageComposeResultCancelled: 
      feedbackMsg.text = @"Result: SMS sending canceled"; 
      break; 
     case MessageComposeResultSent: 
      feedbackMsg.text = @"Result: SMS sent"; 
      break; 
     case MessageComposeResultFailed: 
      feedbackMsg.text = @"Result: SMS sending failed"; 
      break; 
     default: 
      feedbackMsg.text = @"Result: SMS not sent"; 
      break; 
    } 
    [self dismissModalViewControllerAnimated:YES]; 
} 

@end 

的問題是很明顯,我不知道如何與(如果buttonIndex == 1)等的代碼打開郵件和短信

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { 
    if(buttonIndex == 0) { 
    AppSupportView *controller = [[AppSupportView alloc] initWithNibName:@"AppSupportView" bundle:nil]; 
    controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; 
    [self presentModalViewController:controller animated:YES]; 
    [controller release]; 
    } 

    if(buttonIndex == 1) { 

    } 

    if(buttonIndex == 2) { 

    } 

} 

任何幫助,將不勝感激繼續。

感謝

回答

1

看起來像你所有需要的方法有已經.. 只需添加[self showMailPicker:nil][self showSMSPicker:nil]

if(buttonIndex == 1) { 

} 

if(buttonIndex == 2) { 

} 

如果您從上第二個按鈕是您的短信按鈕,添加showSMSPicker到buttonIndex == 1

+0

非常感謝你,我知道這會很簡單。我以前把[自己showMailPicker];但沒有包含:無,並得到一個方法不存在,與應用程序崩潰恢復爲ID錯誤。這工作完美。謝謝 – 2011-06-05 21:31:22

+0

如果你不使用Interface Builder中的這些方法,你也可以將方法聲明從「 - (IBAction)showMailPicker:(id)sender;」到「 - (void)showMailPicker;」..然後你可以用[self showMailPicker]來調用這些方法; – Sascha 2011-06-05 21:33:49

+0

啊,那是我的錯誤。我確實將它們從IBAction更改爲void,但是我離開了:(id)sender;在上面。再次感謝。 – 2011-06-05 21:38:40

相關問題