Search code examples
iphoneobjective-cios6facebook-ios-sdk

How to remove association from a view and a view controller?


When I try to show the facebook share after having sent an email (using MFMailComposeViewController) I get this error:

A view can only be associated with at most one view controller at a time! View [EAGLView] is associated with [EmailViewController]. Clear this association before associating this view with [FacebookView].'

[EmailViewController removeFromParentViewController]; Does nothing

EmailViewController.view = nil; Causes a white screen, even though the email form is long gone.

How to make it forget that I ever sent and email and make the view hierarchy go back to how it was before? The facebook share works if I haven't sent an email.

-(IBAction)ShowEmailForm:(char*)pSubject :(char*)pBody :(char*)pTo
{

    Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));

    if (mailClass != nil)
    {
        if ([mailClass canSendMail])
        {
            self.view = eaglView;

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

            [picker setSubject:[NSString stringWithFormat:@"%s", pSubject]];

            // Set up recipients
            if( pTo != nil )
            {
                NSArray *toRecipients = [NSArray arrayWithObject:[NSString stringWithFormat:@"%s", pTo]];
                [picker setToRecipients:toRecipients];
            }

            // Fill out the email body text
            [picker setMessageBody:[NSString stringWithFormat:@"%s",pBody] isHTML:YES];

            [self presentViewController:picker animated:YES completion:^(){}];
            [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 
{    
    [self dismissViewControllerAnimated:YES completion:^(){ printf("Email form done dismissing.\n"); }];

    printf("Email form dismissed.\n");

    [self removeFromParentViewController];

    //Email was sent.
    if (result == MFMailComposeResultSent)
    {
        printf("Email Sent!\n");

        NSString *pEmail = [self findEmailAddresses:controller.view : 0];

    }
}

Solution

  • Fixed it.

    Change

            self.view = eaglView;
    

    to

            [eaglView addSubview:self.view];