Reputation:
I want to use UIImagePickerController to load photos from the photolibrary of the IPad Application. I am using the following line of code :
-(IBAction)photolibrarypressed:(id)sender{
// / Create window
//self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
NSLog(@"hi");
// Set up the image picker controller and add it to the view
//imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//[window addSubview:imagePickerController.view];
//UIImagePickerController *picker= [[UIImagePickerController alloc]init];
//picker.delegate = self;
//picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
UIImagePickerController *picker= [[UIImagePickerController alloc]init];
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.delegate = self;
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:picker];
self.popoverController = popover;
popoverController.delegate = self;
[popoverController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
[self presentModalViewController:picker animated:YES];
//[picker release];
imageView = [[UIImageView alloc] initWithFrame:[window bounds]];
// Set up the image view and add it to the view but make it hidden
[window addSubview:imageView];
imageView.hidden = YES;
[window makeKeyAndVisible];
}
But however, I am getting the following error :
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present modally an active controller <splitViewDetailViewController:
I am unable to understand it.
The app is of SplitView Type and I have used WebView and ImageView simultaneously. So,when I click on the button photolibrary I need to go into the ImageView(I suppose).
Can someone please help me sort out the issue ?? I am pretty new to objective C. Thanks.
Upvotes: 1
Views: 5805
Reputation: 4140
You've got the code for the iPhone and iPad running at the same time. If you're on an iPad, you should remove the line
[self presentModalViewController:picker animated:YES];
and if you're on an iPhone or iPod you should remove the line
[popoverController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
Alternatively if you want to run your app on both iPad and iPhone, use an if statement to find out which device it's running on:
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
// Display in a popover for the iPad
[popoverController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
} else {
// Display modally for the iPhone
[self presentModalViewController:picker animated:YES];
}
Upvotes: 1