简体   繁体   中英

Presenting view not appearing properly after MFMessageComposeViewController is presented and then dismissed

I have a View Controller which displays a table view. The VC calls another VC to display the send SMS view to the user, the code for this SMS VC is:

- (void) sendSMSWithBody: (NSString*) body andRecipients: (NSArray*) recipients
{
    MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
    if ([MFMessageComposeViewController canSendText])
    {
        controller.messageComposeDelegate = self;
        controller.body = body;
        controller.recipients = recipients;
        [[UIApplication sharedApplication].delegate.window.rootViewController addChildViewController:self];
        [self presentModalViewController:controller animated:YES];
    }
}

- (void) messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
    [self dismissModalViewControllerAnimated:YES];
    [[UIApplication sharedApplication].delegate.window.rootViewController removeFromParentViewController];    
}

(I know the call to sharedApplication is a bit hacky, but it will suffice for now. the rootViewController is a UINavigationController which has its root controller set to the table view controller)

I am invoking the SMS VC from the table VC like so:

- (void ) viewDidAppear:(BOOL)animated
{
    static BOOL presentedSMSVC = NO;
    if (!presentedSMSVC)
    {
        SendSMSController *sendSMS = [[SendSMSController alloc] init];
        [sendSMS sendSMSWithBody:@"body" 
                   andRecipients:[NSArray arrayWithObject:@"123456789"]];
        presentedRegisterVC = YES;
    }
}

The problem is that after the user sends the SMS the table view cells are not displaying.

I thought maybe I need to refresh the view/table so I added a protocol callback from the 2nd VC to the first which gets invoked when the user sends the SMS, and then within the callback call [self.tableView reloadData] But it made no difference.

So I got rid of the intermediary class and edited the table view to display the SMS view directly like this:

- (void ) viewDidAppear:(BOOL)animated
   {
    static BOOL presentedRegisterVC = NO;
    if (!presentedRegisterVC)
    {
        MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
        if ([MFMessageComposeViewController canSendText])
        {
            controller.messageComposeDelegate = self;
            controller.body = @"body";
            controller.recipients = [NSArray arrayWithObject:@"12345678"];
            [self presentModalViewController:controller animated:NO];
        }
    }
}


- (void) messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
    [self dismissModalViewControllerAnimated:NO];    
}

But now the MFMessageComposeViewController doesn't dismiss (although messageComposeViewController:didFinishWithResult: does get called)

What is the problem with both approaches? Thanks

I faced a similar UI issue. My case was: The controller, let's say controller A, in which I had written the code to present and dismiss the MFMessageComposeController, was not being used directly as the active controller rather I was using A.view as a subview over another controller, say controller B. So writing the following was distorting the view:

[self presentViewController:composeVC animated:YES completion:nil];

But then writing the following solved my issue:

[self.customTabbarNavigation presentViewController:composeVC animated:YES completion:nil];

The customTabbarNavigation, was the controller B, ans was actually the navigation controller which was active when controller A's view was visible.

Same change had to be made for dismissing the composeVC.

For the second variant, I changed to:

[self presentViewController: controller animated: YES completion:nil]; [self dismissViewControllerAnimated:YES completion:nil];

And that worked, haven't tried applying to the first method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM