0

when the bar button item is tapped the popover view shows fine but it does not dismiss when a photo is selected. Am I missing something? What should i do?

-(IBAction)addPhoto:(UIBarButtonItem *)sender
{
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    popover = [[UIPopoverController alloc] initWithContentViewController:imagePicker];
    [popover presentPopoverFromBarButtonItem:sender 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"];

    [popover dismissPopoverAnimated:YES];
}


- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [[picker parentViewController] dismissViewControllerAnimated:YES completion:nil];
}
4

7 回答 7

2

问题是您试图关闭图像选择器控制器,但您需要关闭它所在的弹出框。需要根据它们的呈现方式来关闭它们。

改变:

[self dismissViewControllerAnimated:YES completion:nil];

至:

[popover dismissPopoverAnimated:YES];

如果您已调用dismissViewControllerAnimated:completion:,则将使用调用presentViewController:animated:completion:

于 2012-11-30T03:50:26.043 回答
1

要在从图库中选择照片时关闭 UIPopoverViewController,您需要didFinishPickingMediaWithInfo在选择图像后在方法中添加以下行。

- (void)imagePickerController:(UIImagePickerController *)imagePicker didFinishPickingMediaWithInfo:(NSDictionary *)info {
     [popover dismissPopoverAnimated:YES];
     [imagePicker dismissModalViewControllerAnimated:YES];
}
于 2012-11-30T05:38:12.113 回答
0

你还没有自我设定delegateimagePicker另外,您需要popover在委托方法中将其作为实例变量来解除。

于 2012-11-30T04:35:21.997 回答
0

1) 在你的 .h 中你必须添加<UIImagePickerControllerDelegate> 2) 添加imagePicker.delegate=self;所以你的addPhoto方法应该如下所示:

-(IBAction)addPhoto:(UIBarButtonItem *)sender {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate=self;  //add this line to your code
popover = [[UIPopoverController alloc]
           initWithContentViewController:imagePicker];

[popover presentPopoverFromBarButtonItem:sender
                permittedArrowDirections:UIPopoverArrowDirectionAny
                                animated:YES];
}
于 2012-11-30T04:49:01.587 回答
0

只需给代表UIPopoverControllerDelegate和文件UIImagePickerControllerDelegate.h

于 2012-11-30T05:10:41.110 回答
0

如果有人在使用 UIImagePickerController 时仍然遇到这个臭名昭著的错误:

Assigning to 'id <UINavigationControllerDelegate,UIImagePickerControllerDelegate>' from   incompatible type 'DetailViewController *const __strong'

试着移动

UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;

- (void)viewDidLoad{
  //do your stuff
}

并且不要忘记在相应的 .h 文件中包含委托协议定义 (<UINavigationControllerDelegate,UIImagePickerControllerDelegate>)。

这解决了我的问题,希望它会帮助别人。不知道它是否是最好的解决方案,但在类似的情况下对我有用。

于 2013-07-05T16:01:32.643 回答
0

您必须将 UIImagePickerControllerDelegate 添加到相应的 .h 文件中,并检查此代码

 -(IBAction)addPhoto:(UIBarButtonItem *)sender 
{
  UIImagePickerController *imagePicker3 = [[UIImagePickerController alloc] init];
  imagePicker3.delegate = self;
  imagePicker3.sourceType = UIImagePickerControllerSourceTypeCamera;
  imagePicker3.allowsEditing = YES;
  imagePicker3.mediaTypes = [NSArray arrayWithObject:@"public.image"]
  [self presentModalViewController:imagePicker3 animated:YES];

}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { 
[self dismissModalViewControllerAnimated:YES];
_imageselected.image=[info valueForKey:UIImagePickerControllerEditedImage];

}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self dismissModalViewControllerAnimated:YES];

 }
于 2012-11-30T05:48:21.060 回答