UIViewController dismissModalViewControllerAnimated: causes main window to disappear - objective-c

I want to present a modal mail dialogue like so in the iPad app:
MFMailComposeViewController* picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:title];
[picker setMessageBody:[NSString stringWithFormat:[self emailBody], title, [link absoluteString]] isHTML:YES];
[self.viewController presentModalViewController:picker animated:YES];
The following delegate is called when the user sends/cancels:
- (void) mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
[self.viewController dismissModalViewControllerAnimated:YES];
}
This works great in portrait mode. In landscape mode the right hand pane of the UISplitViewController completely disappears.

You can only present these from the primary view of your application. In this case, presenting from the UISplitViewController works.

Related

nav bar nonresponsive after UIImagePickerController dismissed

On iOS 7 only, the navigation bar in my app does not respond to any touches after a UIImagePickerController is used and then dismissed (whether a pic has been selected or not). The screen below the navigation bar functions as normal, but it is now impossible to navigate Back in the app; the user is stuck on this screen.
I am launching the UIImagePickerController from code, though the rest of the app is laid out in storyboards.
UIImagePickerController *mediaUI = [[UIImagePickerController alloc] init];
mediaUI.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
mediaUI.mediaTypes = [NSArray arrayWithObject:(NSString *) kUTTypeImage];
mediaUI.allowsEditing = NO;
mediaUI.delegate = self;
[controller presentModalViewController: mediaUI animated: YES];
Thanks in advance for any help.
I hope you are performing these steps correctly!
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
}
And when you are calling
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL];
Cheers!
I noticed that the log was showing "Unbalanced calls to begin/end appearance transitions for" the launching controller. I was launching the image picker controller immediately from another controller when it appeared. This works OK on iOS 8 but there needs to be a delay on iOS 7. I fixed it by calling my method after a brief delay:
[self performSelector:#selector(takePicture) withObject:nil afterDelay:.1];

Cancel button does not dismiss ComposeMail window in iOS simulator

My implementation is pretty simple:
In the .h file, I'm implementing MFMailComposeViewControllerDelegate
And in the .m file, I have the following bit of code:
-(void)MailCurrentViewAsAttachment
{
if ( [MFMailComposeViewController canSendMail] ) {
MFMailComposeViewController * mailComposer = [[MFMailComposeViewController alloc] init];
mailComposer.delegate = self;
[mailComposer addAttachmentData:imageData mimeType:#"image/jpeg" fileName:#"attachment.jpg"];
[self presentViewController:mailComposer animated:YES completion:nil];
}
}
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
[self dismissViewControllerAnimated:YES completion:nil];
}
The variable imageData above is of the UIImage type, and I know for sure there's nothing wrong with it: the required image shows up properly in the compose mail window.
However clicking the Cancel button does not dismiss the Compose window. What am I missing?
Note: I'm using iOS 6 with the latest version of xcode, and my app is a Universal app.
You are setting the wrong delegate. You want:
mailComposer.mailComposeDelegate = self;
MFMailComposeViewController extends UINavigationController. So setting delegate is for the UINavigationControllerDelegate.

UIPopoverController and UIImagePickerController crash

Here is the set up of my view:
When the UIBarButtonItem is clicked, it should bring up a UIImagePickerController. I have to do this using a UIPopoverController, which is invoked by clicking on the "reset" button, because it is necessary on the iPad. Here is my code:
-(IBAction) btnReset:(id)sender {
[self chooseImage];
}
-(void) chooseImage {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
imagepicker = [[UIImagePickerController alloc] init];
imagepicker.allowsEditing = NO;
imagepicker.delegate = self;
imagepicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagepicker.navigationBar.opaque = true;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
popoverController = [[UIPopoverController alloc] initWithContentViewController:imagepicker];
[popoverController presentPopoverFromBarButtonItem:reset permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
} else {
[self presentModalViewController:imagepicker animated:YES];
}
}
}
However, when this is called, the view crashes with the error:
'NSInvalidArgumentException', reason: '-[UIPopoverController presentPopoverFromRect:inView:permittedArrowDirections:animated:]: Popovers cannot be presented from a view which does not have a window.'
What am I doing wrong? Thank you in advance.
It looks like you are trying to create a popover on an item which is not on the view hierarchy. If this method is being invoked by your button then change the method header to -(void) chooseImage:(id)sender and try presenting the popover from the UIBarButton you have on your toolbar.
Also if you are using ARC (which it looks like you are) you have to hold on to your UIPopover otherwise it will be released when it is still required see this stack overflow post. You may already be doing this but I thought I would raise it as a I can't see if/how you have specified your popoverController.

presentModalViewController:picker not working in iPad MFMailComposeViewController

i am using the following code for the mail composer sheet in iPad application. I used the same code for iPhone. It worked.
I am writing the game in iPad using cocos2d. The game is in landScape mode. The control in EmailScene is stopping at [picker presentModalViewController:picker animated:YES]; It is not giving any error. Should I change my code for iPad ?
#interface EmailScene : CCScene <MFMailComposeViewControllerDelegate>
{
MFMailComposeViewController *picker;
}
-(void)displayComposerSheet;
#end
#implementation EmailScene
- (id) init {
self = [super init];
if (self != nil) {
[self displayComposerSheet];
}
return self;
}
// Displays an email composition interface inside the application. Populates all the Mail fields.
-(void)displayComposerSheet
{
[[CCDirector sharedDirector] pause];
picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
//Fill in the email as you see fit
NSArray *toRecipients = [NSArray arrayWithObject:#"srikanth.rongali786#gmail.com"];
[picker setToRecipients:toRecipients];
//display the view
[[[CCDirector sharedDirector] openGLView] addSubview:picker.view];
[[CCDirector sharedDirector] stopAnimation];
//When I commented the following two lines the mail page is opening.
//[picker presentModalViewController:picker animated:YES];
//[picker release];
}
But, the problem is my game is in landscape mode and the mail sheet is displayed in portrait mode.
Thank you.
You are using -presentModalViewController:… wrongly. This method should be called on the topmost view controller before the "picker" is presented.
[topmostViewController presentModalViewController:picker animated:YES];
(You shouldn't add picker.view as a subview of the -openGLView either.)

imagepicker+navigationController causing problems in iphone?

how do i do this..i have appliation delegate...in which i added uinavigation controller through code...
here is my code...
- (void)applicationDidFinishLaunching:(UIApplication *)application {
HomeScreenController *homeScreenobj=[[HomeScreenController alloc] initWithNibName:#"HomeScreen" bundle:[NSBundle mainBundle]];
navController = [[UINavigationController alloc] initWithRootViewController:homeScreenobj];
[homeScreenobj release];
[window addSubview:navController.view];
// Override point for customization after app launch
[window makeKeyAndVisible];
}
on homescreen i have a tabbar with three buttons here is an image
i want to take photo from library from imagepickerController...but getting weired issues
it is added to the main window i don't know why
here is an image for this
why this isn't shown fullscreen.... and also if i put this line
imagePickerController.allowsImageEditing = YES;
it hangs don't know why..here is my code to takePhoto....
-(IBAction)TakePhotoClicked
{
imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.allowsImageEditing = YES;
imagePickerController.delegate = self;
imagePickerController.sourceType =
UIImagePickerControllerSourceTypePhotoLibrary;
//self.navigationController.navigationBar.hidden=YES;
[self.navigationController pushViewController:imagePickerController animated:NO];
//[imagePickerController release];
}
if i don't put this line to commented
//self.navigationController.navigationBar.hidden=YES;
it shows like this
where clicking on home button take back to home...
You have to present the UIImagePickerController as a modal view controller. So don't push your controller, but present it as modal:
[self.navigationController presentModalViewController:imagePickerController animated:YES];
You can pop the image picker using -[UINavigationController dismissModalViewControllerAnimated:] to go back to the last view controller on the stack.