Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Dismiss view controller?

When I send SMS using the code below the view gets frozen and the user are not being pushed back to the previous view. Why is that?

 - (IBAction)sendSMS:(id)sender {
    MFMessageComposeViewController *controller =
    [[[MFMessageComposeViewController alloc] init] autorelease];

   if([MFMessageComposeViewController canSendText])
  {
      NSString *str= @"";
      controller.body = str;
       controller.recipients = [NSArray arrayWithObjects:
                             @"", nil];
       controller.delegate = self;
       [self dismissViewControllerAnimated:YES
                               completion:nil];
       [self presentViewController:controller animated:YES completion:nil];
    }}  

5 Answers

Ferhat Ezizi
Ferhat Ezizi
10,372 Points

Oh, I misunderstood. You can dismiss a MFMessageComposeViewController inside its delegate method. Try this:

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

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

        case MessageComposeResultSent:
            break;

        default:
            break;
    }

    [self dismissViewControllerAnimated:YES completion:nil];
}

Happy coding!

Ferhat Ezizi
Ferhat Ezizi
10,372 Points

You cannot present a view controller while dismissing the current one. Try this:

[self.presentingViewController dismissViewControllerAnimated:YES completion:^{
    [anotherView presenViewController:controller animated:YES];
}];

Or you can just use protocols&delegate to handle this.

Or with completion blocks: http://stackoverflow.com/questions/12658147/present-and-dismiss-uiviewcontroller-with-completion-blocks-without-protocols

Thanks man!! But I don't need to dismiss the current one to present a totally new one. I just want to get pushed back. My problem is that when I send and sms the view gets frozen and not pushing back if he/she wants to send a another SMS

Thanks!! :)

It still gets frozen in the mobile with the code you posted...:(