0

MFMessageComposeViewControllerDelegate 프로토콜을 준수하는 VC가 있습니다.MFMessageComposeVIewController 자체 대신 대리자를 닫습니다.

나는 성공적으로 다음 코드로이 뷰 컨트롤러를 제시하고있다 :

- (IBAction)textAction:(id)sender { 
    if(![MFMessageComposeViewController canSendText]) 
    { 
     UIAlertView *warningAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Your device doesn't support SMS!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
     [warningAlert show]; 
     return; 
    } 

    NSString *numberToCallOrText = self.phoneNumber; 
    NSString *message = @"Test message"; 
    NSArray *recipients = [NSArray arrayWithObject:numberToCallOrText]; 
    MFMessageComposeViewController *messageController = [[MFMessageComposeViewController alloc] init]; 
    messageController.messageComposeDelegate = self; 
    [messageController setRecipients:recipients]; 
    [messageController setBody:message]; 

    // Present message view controller on screen 
    [self.view endEditing:YES]; 
    [self presentViewController:messageController animated:YES completion:nil]; 
} 

을 또한, 내가 지금 같은 마무리 결과를 처리하고있다 :

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult) result 
{ 
    switch (result) { 
     case MessageComposeResultCancelled: 
      NSLog(@"Canceled"); 
      break; 

     case MessageComposeResultFailed: 
     { 
      NSLog(@"Failed"); 
      UIAlertView *warningAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Failed to send SMS!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
      [warningAlert show]; 
      break; 
     } 

     case MessageComposeResultSent: 
      NSLog(@"sent"); 
      [self.navigationController popViewControllerAnimated:YES]; 
      break; 

     default: 
      break; 
    } 
    [controller.view endEditing:YES]; 
    [self.navigationController popViewControllerAnimated:YES]; 
// [self dismissViewControllerAnimated:YES completion:nil]; 
// [controller popToViewController:self animated:YES]; 
// [controller dismissViewControllerAnimated:YES completion:nil]; 
} 

세 주석 라인이 대안을하는 I 시도했었다. MFMessageComposeViewController가 화면에 남아 있지만 (키보드가 닫히지 만) 대리자가 스택에서 튀어 나오고 있습니다. 따라서 다시 취소 할 때 null 참조 오류가 발생합니다.

이 구현은 코드 내 다른 곳에서 작동하기 때문에 이상하지 않습니다. 유일한 차이점은 초기화 할 본문을 설정했기 때문입니다.

왜 잘못된 VC가 여기 갑자기 터지게 되었는가?

감사합니다.

편집 - 깨진 구현은 UIView Controller가 아닌 UITableViewController에 있습니다. 문제의 원인이 될 수 있습니까?

+0

TableVC에 대한 편집과 별도로 모든 것이 (한 지점에서) 정확히 같은 방식으로 구현되었습니다. 'setBody' 호출도 주석 처리했습니다. 그것은 여전히 ​​애플 리케이션의 다른 곳에서 작동합니다. 취소 버튼이 작동하지 않는 것은 바로 여기에 있습니다. –

답변

0

문제는 대신의 UIViewController의있는 UITableViewController에서 MFMessageComposeViewController를 호출 줄기 나타납니다 .... 당신의 도움을 바랍니다. 따라서 내 솔루션 대신 MFMessageComposeViewController 밀어 및 MFMessageComposeViewController 완료 핸들러 자체를 해제하는 유일한 목적은 다른보기 컨트롤러 밀어 테이블을했습니다.

0

presentViewController:animated:completion:을 사용하여 메시지 작성기를 제공하고 있습니다. 이 방법으로 뷰 컨트롤러가 제공되면 dismissViewControllerAnimated:completion:을 사용하여 해제해야합니다.

하지만 popViewControllerAnimated:을 사용하면 네비게이션 컨트롤러가 네비게이션 스택 맨 위에있는보기 컨트롤러를 닫도록 지시하고 있습니다. 이것은 메시지 작성자가 아니라 메시지 작성자입니다.

주석 처리 된 행 중 하나에 가까이있었습니다. 또한 선을 제거해야

[controller dismissViewControllerAnimated:YES completion:nil]; 

:

[self.navigationController popViewControllerAnimated:YES]; 

로 : 당신은 교체해야

[self.navigationController popViewControllerAnimated:YES]; 

메시지를 보낼 때. 여분의 해고를하지 마십시오.

+0

내 주석 처리 된 행 중 하나입니다. 두 번째 것은 사실,'[self dismissViewControllerAnimated : YES completion : nil]; 이후에 시도했는데, 이것은 내가 가지고있는 다른 구현에 대해 작업하고있는 것입니다.바로 presentViewController와 쌍을 이룬다. https://developer.apple.com/reference/messageui/mfmessagecomposeviewcontroller?language=objc –

+0

그리고 여기에도 애플이 가지고있는 문서가있다. 또한 메시지 작성자 대리자 메소드에서'endEditing :'에 대한 호출을 제거해야합니다. – rmaddy

+0

여기서는 작동하지 않습니다. 'endDditing :'이후에'[self dismissViewController : ..'와'[controller dismissViewController : ...'를 시도해 보았습니다. 왜냐하면이 tableVC가 호출 된 것을 알기 위해 중단 점을 설정하지 않았기 때문에, 그래서 나는 키보드가 해산되는 것을 어떻게 든 해고 요청이라고 생각했다. –

0

코드는 아래를 참조하시기 바랍니다는

-(void) showEmailModalView  
{ 
    if ([MFMailComposeViewController canSendMail]) 
    { 
     MFMailComposeViewController* controller 
     = [[MFMailComposeViewController alloc] init]; 
     controller.mailComposeDelegate = self; 
     [controller setToRecipients: @[@"[email protected]du.sg"]]; 

     [self presentViewController: controller 
          animated: YES 
         completion: nil]; 
    } 
    else 
    { 
     UIAlertController* alert = [UIAlertController 
      alertControllerWithTitle: @"Email not configured" 
      message: @"Please add/enable an email " 
      @"account in the phone to send email" 
      preferredStyle: UIAlertControllerStyleAlert]; 

     UIAlertAction* ok = [UIAlertAction 
      actionWithTitle: @"Ok" 
      style: UIAlertActionStyleDefault 
      handler: nil]; 

     [alert addAction: ok]; 

     [self presentViewController: alert 
          animated: YES 
         completion: nil]; 
    } 
} 


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

{ 

    switch (result)   
    { 

     case MFMailComposeResultCancelled: 
     break; 
     case MFMailComposeResultSaved: 
     break; 
     case MFMailComposeResultSent: 
     break; 
     case MFMailComposeResultFailed: 
     break; 

     default: 
     { 

     UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Email" message:@"Sending Failed - Unknown Error :-(" preferredStyle:UIAlertControllerStyleAlert]; 

     UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]; 
     [alertController addAction:ok]; 
     [self presentViewController:alertController animated:YES completion:nil]; 


     }    
     break; 
    } 

    [self dismissViewControllerAnimated:controller completion:nil]; 
} 
+0

고마워,하지만 그건 내가 가진 것보다 다소 적다. 나는이 문제가 기본 VC로부터 유래한다고 믿는다. 나는 테이블 뷰 컨트롤러가 아닌 모달로 제시하고있다. 먼저 빈 VC를 MFMessageComposeVC를 열고 해당 델리게이트 역할을하는 스택에 밀어 넣어서 문제를 피할 수있었습니다. 메시지가 완료되면 자체적으로 팝합니다. –