2012-12-11 68 views
0

在我的iphone應用程序中。我正在使用MFMailComposeViewController創建Iphone郵件聊天表。現在,當我第二次輸入郵件時,我的應用程序正在崩潰。我在谷歌搜索。但我找不到解決方案。任何人都可以幫我解決我的問題。請看下面的代碼,並幫助我在哪裏做錯了。當我輸入iphone郵件表時,iPhone應用程序崩潰

if ([MFMailComposeViewController canSendMail]) 
     { 
      controller = [[MFMailComposeViewController alloc] init]; 
      controller.mailComposeDelegate = self; 
      [controller setSubject:@""]; 
      [controller setToRecipients:array1]; 
      [controller setMessageBody:@"" isHTML:NO]; 
      [controller setMailComposeDelegate: self]; 
      [controller setModalTransitionStyle:UIModalTransitionStyleCrossDissolve]; 
      [self.navigationController presentModalViewController:controller animated:NO]; 
      [controller release]; 
     } 

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error 
{ 
    if(result == MFMailComposeResultSent) 
    { 
     [[self parentViewController] dismissModalViewControllerAnimated:YES]; 
    } 
    else if (result == MFMailComposeResultCancelled) 
    { 
     [[self parentViewController] dismissModalViewControllerAnimated:YES]; 
    } 
} 
+0

您正在使用哪個版本的xcode? – Rushabh

+0

@Rocks感謝您的回覆。 IOS5。 – Gopinath

+0

你的NSLog顯示什麼錯誤? –

回答

0

我覺得這個代碼使問題:

[[self parentViewController] dismissModalViewControllerAnimated:YES]; 

代替上述使用的:

[self dismissModalViewControllerAnimated:YES]; 
+0

感謝您的回覆。抱歉。我得到了這個問題。現在,當我第二次進入郵件表時,點擊發送按鈕。我的應用程序崩潰。我能做什麼。這個問題的問題是什麼? – Gopinath

+2

@Gopinath,你能給我們碰撞記錄嗎? – Ilanchezhian

+0

@Gopinath:請檢查您的導航控制器的視圖類 –

0

嘗試寫此委託方法,而不是你writen同樣的方法

並讓我知道它是否在工作!

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error 
{ 
switch (result) 
{ 
    case MFMailComposeResultCancelled: 
     NSLog(@"canceled"); 
     break; 
    case MFMailComposeResultSaved: 
     NSLog(@"saved"); 
     break; 
    case MFMailComposeResultSent: 
     NSLog(@"sent"); 
     [self showErrorMessage: @"Email sent successfully"]; 
     break; 
    case MFMailComposeResultFailed: 
     NSLog(@"failed"); 
     [self showErrorMessage: @"Failed to send email"]; 
     break; 
    default: 
     NSLog(@"not sent"); 
     break; 
} 
[self dismissViewControllerAnimated:YES completion:nil]; 

} 

快樂編碼!!!!!!!

+0

感謝您的幫助。但是當我在郵件表中輸入secound時,我點擊發送的按鈕。那麼我的應用程序崩潰。但在我的控制檯中,我得到了這個錯誤。 - [myviewcontroler showErrorMessage:]:無法識別的選擇發送到實例0x883a400 – Gopinath

0

替換此代碼:

[self.navigationController presentModalViewController:controller animated:NO]; 

有:

[self presentViewController:controller animated:YES completion:nil]; 
+0

感謝reply.Sorry。我在控制檯中遇到了這個錯誤。 - [myViewCotroler showErrorMessage:]:無法識別的選擇器發送到實例0x9839a00 – Gopinath

+0

@Gopinath,刪除''self showErrorMessage:'並檢查該行。 – iDev

+0

@ACB我可以知道這個問題嗎? – Gopinath

0

的方法,您正在打開的郵件視圖,用下面的替換代碼code.and告訴我,無論是工作還是不!!!!!!快樂編碼!!!!

MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init]; 

if ([MFMailComposeViewController canSendMail] == NO) { 
     return; 
} 
else 
{ 
    picker.mailComposeDelegate = self; 
    NSString *stringtitle=[NSString stringWithFormat:@"Apple"]; 
    [picker setSubject:stringtitle]; 

    NSData *imgDataLoop = UIImagePNGRepresentation(shaereImage.image); 

    [picker addAttachmentData:imgDataLoop mimeType:@"image/png" fileName:@"rainy"]; 

    [self presentViewController:picker animated:YES completion:nil]; 
    [picker release]; 

} 
0

改變你的方法,

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error 
{ 
    switch (result) 
    { 
     case MFMailComposeResultCancelled: 
      NSLog(@"canceled"); 
      break; 
     case MFMailComposeResultSaved: 
      NSLog(@"saved"); 
      break; 
     case MFMailComposeResultSent: 
      NSLog(@"sent"); 
      [self showErrorMessage: @"Email sent successfully"]; 
      break; 
     case MFMailComposeResultFailed: 
      NSLog(@"failed"); 
      [self showErrorMessage: @"Failed to send email"]; 
      break; 
     default: 
      NSLog(@"not sent"); 
      break; 
    } 
    [self dismissViewControllerAnimated:YES completion:nil]; 

} 

-(void)showErrorMessage:(NSString *)iMessage 
{ 
     NSLog(@"%@", iMessage); 
} 

正如我前面提到的,你的方法的簽名是不同的,而調用和定義部分。如上所示,這兩個地方應該是一樣的。

雖然調用方法是[self showErrorMessage: @"Failed to send email"];,但在定義上你不接受字符串部分。你需要有一個相同的輸入參數。你也可以通過調用它作爲[self showErrorMessage];,然後保持showErrorMessage方法。

+0

@Gopinath,這個問題的任何更新? – iDev

0
-(void)mail 
{ 
    if ([MFMailComposeViewController canSendMail]) 
    { 

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

    [mailController setSubject:@"your Subject"]; 
    [mailController setMessageBody:@"your message" isHTML:NO]; 

    UIImage *tmp = [UIImage imageNamed:@"Icon.png"]; 
    NSData *myData = UIImageJPEGRepresentation(tmp, 1.0); 

    [mailController addAttachmentData:myData mimeType:@"image/png" fileName:@"MyPhoto.png"]; 
    if (arrTo) 
     [mailController setToRecipients:arrTo]; 

    [self presentModalViewController:mailController animated:YES]; 
    return YES; 
} 
else { 
    return NO; 
} 
} 
} 
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error 
{ 

    [controller dismissModalViewControllerAnimated:YES]; 
}