2014-01-17 39 views
0

我得到了一個名爲SecondViewController的UIViewController,它使用UIImagePickerController加載照片庫。當我選擇一張照片時,它應該顯示另一個名爲PhotoViewController的視圖控制器。爲了將圖像從SecondViewController傳遞到PhotoViewController,我使用了一個單例。下面是代碼:presentViewController之後的黑屏

SecondViewController:

@property (strong,nonatomic) UIImagePickerController *libraryPicker; 
@property (strong,nonatomic) UIImage *chosenImage; 
@property (strong,nonatomic) PhotoViewController *controller; 

- (void)viewDidLoad 
{ 
     controller = [PhotoViewController sharedInstance]; 
     libraryPicker = [[UIImagePickerController alloc] init]; 
     libraryPicker.delegate = self; 
     libraryPicker.allowsEditing = NO; 
     libraryPicker.navigationBarHidden = NO; 
     libraryPicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; 
     [self presentViewController:libraryPicker animated:YES completion:nil]; 
} 

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
    chosenImage = [info objectForKey:UIImagePickerControllerOriginalImage]; 
    controller.photo = chosenImage; 
    [libraryPicker dismissViewControllerAnimated:NO completion:nil]; 
    [self presentViewController:controller animated:YES completion:nil]; 
} 

PhotoViewController:

@property (strong,readwrite) IBOutlet UIImageView *photoView; 
@property (strong,readwrite) UIImage *photo; 
+ (PhotoViewController *)sharedInstance; 

static PhotoViewController *_self; 
+ (PhotoViewController*)sharedInstance 
{ 
    if (_self == nil) { 
     _self = [[super alloc] init]; 
    } 
    return _self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 
    photoView.image = photo; 
} 

所以,當我選擇一個PIC,應用程序顯示黑屏,而不在調試方面的錯誤。怎麼了?

P.S:我使用的是故事板和ARC

回答

1

幾件事情要注意:

  • 如果PhotoViewController被設計在了故事板,你是實例它sharedInstance的方式是不正確的。它只創建一個沒有創建Interface Builder對象的普通PhotoViewController
    • 這裏的一個更好的方法是將SecondViewControllerPhotoViewController的場景與故事板中的手動模態連接。然後在didFinishPickingMediaWithInfoSecondViewController中,在PhotoViewController上設置照片並手動觸發segue。
  • PhotoViewControllerviewDidLoad將只能被稱爲第一 一次它提出的,所以它不是設置照片 後續啓動正確的地方。考慮photoView連接在故事板
的屬性和接口 生成器元件之間 viewWillAppear
  • 確認
  • 相關問題