簡體   English   中英

UIpopoverController dealloc 達到,而 popover 在 Ipad 中仍然可見

[英]UIpopoverController dealloc reached while popover is still visible in Ipad

我正在使用 popover 顯示相機膠卷,用戶可以選擇圖片並將其顯示在UIImageView

下面的代碼將顯示選擇器,但在單擊圖片時,我得到一個 NSexception。 我在這里查看了類似的問題,並在 google 上找到了解決方案。 我的彈出框設置為強。 任何幫助,將不勝感激

#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
#import <Foundation/Foundation.h>
#import "Pilot.h"

@interface PilotViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate,UIPopoverControllerDelegate>




@property (readonly, nonatomic) UIImage                     *viewImage;

@property (weak, nonatomic    ) IBOutlet UIImageView        *imageView;

@property (nonatomic, strong  ) UIPopoverController         *popoverController;
@property (strong, retain     ) UIImagePickerController     *imagePicker;

//PilotViewController.m

#import "PilotViewController.h"

@interface PilotViewController ()


@end

@implementation PilotViewController
@synthesize popoverController;
@synthesize imagePicker;
@synthesize imageView;






//Button to open library
- (IBAction)library:(id)sender{

    //Create image picker controller

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

    //Set source to the photo library
    self.imagePicker.delegate       = self;
   self. imagePicker.sourceType     = UIImagePickerControllerSourceTypePhotoLibrary;
    self.imagePicker.allowsEditing  = NO;

    self.popoverController          = [[UIPopoverController alloc]
                                       initWithContentViewController:imagePicker];
    self.popoverController.delegate = self;
    CGRect popoverRect = [self.view convertRect:[self.view frame]
                                       fromView:[self.view superview]];

    popoverRect.size.width = MIN(popoverRect.size.width, 80) ;
    popoverRect.origin.x  = popoverRect.origin.x+150;

    [self.popoverController
     presentPopoverFromRect:popoverRect
     inView:self.view
     permittedArrowDirections:UIPopoverArrowDirectionLeft
     animated:YES];
}



-(void) imagePickerController:(UIImagePickerController *)picker didfinishPickingImage:(UIImage *)image
                  editingInfo: (NSDictionary *) editinginfo
{
    imageView.image                = image;
    [self dismissViewControllerAnimated:YES completion:nil];
}






- (IBAction)camera:(id)sender{


    if ([UIImagePickerController isSourceTypeAvailable:
         UIImagePickerControllerSourceTypeCamera])

    {

     //Create image picker controller

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

    self.imagePicker.delegate       = self;
    self.imagePicker.sourceType=
    UIImagePickerControllerSourceTypeCamera;
    self.imagePicker.allowsEditing  = NO;

    self.imagePicker.cameraDevice =
    UIImagePickerControllerCameraDeviceRear;
    [self presentViewController:imagePicker animated:YES completion:nil];
}

    else
    {
    UIAlertView *alert      = [[UIAlertView alloc]
                              initWithTitle:@"Camera failed to open"
                              message:@"Camera is not available"
                              delegate:nil
                              cancelButtonTitle:@"OK"
                              otherButtonTitles: nil];
        [alert show];
    }




}

#pragma mark Image Picker Delegate Methods

//on cancel dimiss picker controller

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


}

//Used when user has chosen an image

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    UIImage * image                 = info[UIImagePickerControllerOriginalImage];
    imageView.image                = image;
    [self dismissViewControllerAnimated:YES completion:nil];
}



    @end





- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    if ([picker sourceType == UIImagePickerControllerSourceTypeCamera]){
        UIImage * image                 = info[UIImagePickerControllerOriginalImage];
        imageView.image                 = image;
        [self dismissViewControllerAnimated:YES completion:nil];


    }else{


   UIImage * image                 = info[UIImagePickerControllerOriginalImage];
   imageView.image                 = image;
}

          }

    @end

你的問題是你在沒有首先關閉彈出窗口的情況下關閉視圖。

您可以確保它被忽略覆蓋viewWillDisappear

-(void) viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    if (self.popoverController != nil) {
        [self.popoverController dismissPopoverAnimated:animated];
        self.popoverController=nil;
    }
 }

您還應該添加 UIPopoverControllerDelegate 方法popoverControllerDidDismissPopover: -

- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController {
    self.popoverController=nil;
}

更新

您在didFinishPickingMediaWithInfo if 語句應該是

if ([picker sourceType] == UIImagePickerControllerSourceTypeCamera){

或者

if (picker.sourceType == UIImagePickerControllerSourceTypeCamera){

你把 ] 放在了錯誤的地方。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM