2012-10-12 125 views
4

我的应用程序是创建csv文件并通过邮件发送它。但是当我丢弃邮件时,我正在变黑屏。之前的观点并未出现,并且正在被黑屏覆盖。我在堆栈溢出中查看了各种问题和答案。但似乎没有任何工作。dismissModalViewControllerAnimated产生黑色背景

- (IBAction)openMail:(id)sender 
{ 
    [self getdata]; 

    if ([MFMailComposeViewController canSendMail]) 
    { 
     MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init]; 
     mailer.mailComposeDelegate = self; 
     [mailer setSubject:@"BMNET- Travel Log"]; 




     NSString *[email protected]"Name, StartingDateNTime, EndingDateNTime, TravelType, DistanceTravelled, Amount\n" ; 

     NSString *CSVPath,*record;; 

     NSString *temporayCSV= @"" ; 

      for (int i=0; i<[getAmount count]; i++) {  

       record = [NSString stringWithFormat:@"%@, %@, %@, %@, %@, %@", [getName objectAtIndex:i], [getStartDate objectAtIndex:i], [getEndDate objectAtIndex:i],[getType objectAtIndex:i],[getDistance objectAtIndex:i],[getAmount objectAtIndex:i]]; 



         NSLog(@"%d",i); 

         temporayCSV = [NSString stringWithFormat:@"%d %@ \n ",(i+1),record]; 

         CSVstring = [CSVstring stringByAppendingFormat:temporayCSV];  
         NSLog(@"%@",CSVstring); 

      } 

     NSArray *paths = NSSearchPathForDirectoriesInDomains(
                  NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString *documentsDirectory = [paths objectAtIndex:0]; 

     NSArray *toRecipients = [NSArray arrayWithObjects:@"", nil]; 
     [mailer setToRecipients:toRecipients]; 
     CSVPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.csv", @"CSV_FormatedTable"]]; 
     NSFileManager *fileManager; 
     //add our file to the path 
     [fileManager createFileAtPath:CSVPath contents:[CSVstring dataUsingEncoding:NSUTF8StringEncoding] attributes:nil]; 
     NSData *rolesCSVData =[NSData dataWithContentsOfFile:CSVPath]; 
     NSLog(@"The data is %@",CSVstring); 

     //create my data to append 
     NSFileHandle *handle; 
     handle = [NSFileHandle fileHandleForWritingAtPath: CSVPath ]; 
     //say to handle where's the file fo write 
     [handle truncateFileAtOffset:[handle seekToEndOfFile]]; 
     //position handle cursor to the end of file 
     [handle writeData:[data dataUsingEncoding:NSUTF8StringEncoding]]; 
     //write data to with the right encoding 






     [mailer addAttachmentData:rolesCSVData mimeType:@"text/csv" fileName:@"Log"]; 
     NSString *emailBody = @"Attachment of Log"; 
     [mailer setMessageBody:emailBody isHTML:NO]; 
     [self presentModalViewController:mailer animated:YES]; 
     mailer.modalPresentationStyle = UIModalPresentationPageSheet; 

    } 
    else 
    { 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failure" 
                 message:@"Your device doesn't support the composer sheet" 
                 delegate:nil 
               cancelButtonTitle:@"OK" 
               otherButtonTitles:nil]; 
     [alert show]; 
    } 
} 

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error 
{ 
    switch (result) 
    { 

     case MFMailComposeResultCancelled: 
      NSLog(@"Mail cancelled: you cancelled the operation and no email message was queued."); 

      break; 
     case MFMailComposeResultSaved: 
      NSLog(@"Mail saved: you saved the email message in the drafts folder."); 

      break; 
     case MFMailComposeResultSent: 
      NSLog(@"Mail send: the email message is queued in the outbox. It is ready to send."); 

      break; 
     case MFMailComposeResultFailed: 
      NSLog(@"Mail failed: the email message was not saved or queued, possibly due to an error."); 

      break; 
     default: 
      NSLog(@"Mail not sent."); 

      break; 
    } 
    // Remove the mail view 

    [self dismissModalViewControllerAnimated:YES]; 

} 

回答

1

presentModalViewController:animated:dismissModalViewControllerAnimated:被弃用iOS6的,就像一个侧面说明! 您定位的是哪个iOS版本,并且这是您正在使用的iPhone或iPad应用程序?

那旁边,我注意到以下几点:

[self presentModalViewController:mailer animated:YES]; 
mailer.modalPresentationStyle = UIModalPresentationPageSheet; 

你设置modalPresentationStyle你提出的视图控制器后! 在presentModalViewController行之前移动行mailer.modalPresentationStyle = UIModalPresentationPageSheet;。也许这是问题!

+1

非常感谢Nenad ..我知道了..对不起,我的声望小于15,因此我无法投票给你..你说的概率是与mailer.modalPresentationStyle = UIModalPresentationPageSheet; –

+0

如果你愿意,你应该可以投票了! :-) –

+0

当然Nenad ..非常感谢你..你知道我的问题的答案 - [链接](http://stackoverflow.com/questions/12909910/iphone-maximumelapsedtimeforcachedlocation-used-to-remove-cache -value-of-core-l) –