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!
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

YOL O!
57 PointsDismiss 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
10,372 PointsOh, 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
10,372 PointsYou 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

YOL O!
57 PointsThanks 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

YOL O!
57 PointsThanks!! :)

YOL O!
57 PointsIt still gets frozen in the mobile with the code you posted...:(