Add A UIButton In A PopOverController - Xcode - objective-c

I have an app which has a toolbar at the bottom with one of the options as Upload. On clicking on it, a PopOverController occurs which displays the list of photos to choose from.
After choosing a photo, I want to display a view (inside the PopOverController itself)which contains the photo alongwith the button called as Upload. On choosing my photo, currently my PopOverController is not getting dismissed. I have used the following lines of code :
-(IBAction)photolibrarypressed:(id)sender{
NSLog(#"hi");
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];
imageView = [[UIImageView alloc] initWithFrame:[window bounds]];
[window addSubview:imageView];
imageView.hidden = YES;
[window makeKeyAndVisible];
}
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)image
editingInfo:(NSDictionary *)editingInfo
{
imageView.image = image;
NSLog(#"hellow");
[self dismissModalViewControllerAnimated:YES];
// need to show the upload image button now
upload.hidden = NO;
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissModalViewControllerAnimated:YES];
exit(0);
}
Can Someone help me to sort out the issue ??

Instead of calling
[self dismissModalViewControllerAnimated:YES];
call the same cancel method as int eh cancel method to handle dismissing it's modal view
[picker dismissModalViewControllerAnimated:YES]; // Dismisses modalView

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];

Segue not appearing after Image Picker

I am writing an iOS app where, in a view there is a button. Clicking on that button gets an action sheet which has options to pick image from camera or gallery. After the image is picked, it should be passed on to another view by manually calling a segue. The image picker appears and the prepare for segue code is executed, but the segue doesnt appear. There are no errors and I have checked all identifiers etc. Here is the code:
-(IBAction)showButtonClicked:(id)sender {
UIActionSheet *photoActionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Take Photo", #"Choose from Library", nil];
photoActionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
[photoActionSheet showInView:self.tabBarController.tabBar];
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
switch (buttonIndex) {
case 0:
{
[self dismissModalViewControllerAnimated:YES];
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:picker animated:YES];
break;
}
case 1:
{
[self dismissModalViewControllerAnimated:YES];
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:picker animated:YES];
break;
}
default:
break;
}
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"goToPhotoDetails"]){
FostoPhotoDetailsViewController *photoDetails = (FostoPhotoDetailsViewController *)segue.destinationViewController;
photoDetails.imageView.image = self.buttonImage1;
}
}
- (void) imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)image
editingInfo:(NSDictionary *)editingInfo
{
self.buttonImage1 = image;
//FostoPhotoDetailsViewController *photoDetails = [[FostoPhotoDetailsViewController alloc] init];
//photoDetails.imageView.image = image;
[self dismissModalViewControllerAnimated:NO];
NSLog(#"Test");
[self performSegueWithIdentifier:#"goToPhotoDetails" sender:self];
}
Sounds like you are executing a segue with style push without being inside a UINavigationController.
When you call performSegueWithIdentifier:sender: it first correctly calls prepareForSegue:sender: then it tries to retrieve the current navigation controller and push the destination controller doing something like
[self.navigationController pushViewController:destinationController animated:YES];
But since you are not inside a UINavigationController the property navigationController is set to nil, causing the above call to fail silently.
Either change the display style of your segue to something other than push (for instance modal) or embed you controller in a UINavigationController.

Image Picker on ipad

I know image picker on the iPad needs to be a popover, and I keep trying but it won't work and crashes when I tap the button that calls the popover.
-(IBAction)addPhoto:(id)sender{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
popover=[[UIPopoverController alloc]
initWithContentViewController:imagePicker];
[popover presentPopoverFromRect:((UIButton *)sender).frame
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)selectedImage editingInfo:(NSDictionary *)editingInfo {
// Delete any existing image.
NSManagedObject *oldImage = imageClass.image;
if (oldImage != nil) {
[imageClass.managedObjectContext deleteObject:oldImage];
}
// Create an image object for the new image.
NSManagedObject *myImage = [NSEntityDescription insertNewObjectForEntityForName:#"Image" inManagedObjectContext:imageClass.managedObjectContext];
imageClass.image = myImage;
// Set the image for the image managed object.
[image setValue:selectedImage forKey:#"image"];
[self dismissViewControllerAnimated:YES completion:nil];
}
Based on the exception you are getting, the problem is that the sender for the addPhoto method is actually a UIBarButtonItem, not a UIButton. You need to change how the popover is displayed from using the button's frame to being displayed from the bar button.
-(IBAction)addPhoto:(UIBarButtonItem *)sender {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
popover = [[UIPopoverController alloc]
initWithContentViewController:imagePicker];
[popover presentPopoverFromBarButtonItem:sender
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
}
Try to create property to store Your popover controller or store content controller. Seems that one of view controllers releases right after end of method. Also, iOS 7 have awful behavior of UIImagePickerController in popover.
This is related question.

UIimagepickercontroller

I am using UIimagepickercontroller to browse or take a photo and display it on a subview.
-(IBAction) getPhoto:(id) sender {
secondView1 = [[secondView alloc]
initWithNibName:#"secondView"
bundle:nil];
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
if((UIButton *) sender == choosePhotoBtn) {
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
} else {
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
}
[self presentModalViewController:picker animated:YES];
[self.view addSubview:secondView1.view];
}
This works fine for SourceTypeSavedPhotoAlbum, but if I use camera, the secondview1.view does not show. Instead it only shows the original view.
This is my delegate method:
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info {
secondView1.imageView.image = [info
objectForKey:#"UIImagePickerControllerOriginalImage"];
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
[picker release];
}
Thank you for any help!
I finally found out that I should do:
secondView1 = [[secondView alloc]
initWithNibName:#"secondView"
bundle:nil];
self.view addSubview:secondView1.view];
in the delegate function.
(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
...
}
Otherwise, the addsubview could be called before the image picker finishes picking an image. Now understand better about how the code flows and the delegate functions.

Dismiss an add contact view controller from a UIPopover

Hi In my app I have a button so when you click it a UIPopover comes up with an add contact view in it. It all workers except when you press save. It doesn't Dismiss.
-(IBAction) addcontact
{
ABNewPersonViewController *contacts = [[ABNewPersonViewController alloc] init];
// imagePicker.delegate = self;
// UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:contacts];
UINavigationController *addContactNavController = [[UINavigationController alloc] initWithRootViewController:contacts];
popover = [[UIPopoverController alloc] initWithContentViewController:addContactNavController];
popover.popoverContentSize = CGSizeMake(320, 1000);
[popover presentPopoverFromRect:CGRectMake(935, 270, 175, 300)
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionRight
animated:YES];
[popover retain];
[addContactNavController release];
[contacts release];
}
Implement the ABNewPersonViewControllerDelegate protocol and assign the delegate in your method above –
contacts.newPersonViewDelegate = self;
You can then dismiss the popover in the delegate function –
- (void)newPersonViewController:(ABNewPersonViewController *)newPersonView
didCompleteWithNewPerson:(ABRecordRef)person {
[popOver dismissPopoverAnimated:YES];
}
- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController {
[popOver release];
}
well,
[self dismissModalViewControllerAnimated:YES];
respecively
[popover dismissPopoverAnimated:YES];
should do it?
Edit: to be more concrete:
....
popover = [[UIPopoverController alloc] initWithContentViewController:addContactNavController];
addContactNavController.delegate = self;
now on saving, do something like:
-(IBAction) saveStuff {
... saving...
[delegate closePopup];
}
and in your File with -(IBAction) addcontact you do:
-(void) closePopup {
[self dismissModalViewCotroller...];
}
makes sense?
and yes, you should add a delegate-propery to your controller if not done yet