2014-11-14 14 views
0

我需要在用戶輸入ViewControllerA時顯示圖像選取器,並在用戶製作照片並按下「使用照片」按鈕後停留在那裏。無法在View Controller中關閉圖像選取器

實際上,當用戶點擊「使用照片」按鈕時,問題就出來了,在這種情況下,圖像選擇器會一次又一次地彈出。我知道這是因爲viewWillAppear,但viewDidLoad也不是一個好的選擇,因爲在這種情況下圖像選擇器將不會出現超過1次。

可能有人可以給我一些指導,我如何關閉圖像選擇器沒有這個問題?我只想在用戶每次進入視圖控制器時顯示它,並在用戶選擇圖像後關閉它。

- (void)viewWillAppear:(BOOL)animated { 

    [super viewWillAppear:animated]; 
    self.imagePicker = [[UIImagePickerController alloc] init]; 
    self.imagePicker.delegate = self; 
    self.imagePicker.allowsEditing = YES; 

    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { 
     self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera; 
    } 
    self.imagePicker.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeImage]; 

    [self presentViewController:self.imagePicker animated:NO completion:nil]; 
} 

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

    UIImage *choosenImage = info[UIImagePickerControllerEditedImage]; 
    self.placeholderImg.image = choosenImage; 
    [self dismissViewControllerAnimated:YES completion:nil]; 

} 

- (void) imagePickerControllerDidCancel:(UIImagePickerController *)picker { 
    [self dismissViewControllerAnimated:NO completion:nil]; 
    [self.tabBarController setSelectedIndex:1]; 
} 
+0

呼叫'解僱...''上的picker'代替'self'。 – rmaddy 2014-11-14 22:55:34

+0

@rmaddy我在'didFinishPickingMediaWithInfo:'中調用它,或者我應該在別處做? – rihe 2014-11-14 23:00:08

+0

只需用'[picker dismiss ...'替換'[self dismiss ...']。 – rmaddy 2014-11-14 23:01:43

回答

0

在您的視圖控制器的.m文件,你可以聲明BOOL,將跟上,如果你提出的UIImagePickerController了。

@interface MyCustomViewController() 
{ 
    BOOL imagePickerHasBeenPresented; 
} 

@end 

,然後在viewWillAppear方法檢查布爾:

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 

    if (imagePickerHasBeenPresented == NO) 
    { 
     imagePickerHasBeenPresented = YES; 
     //Code to present imagePicker 
    } 
} 
+0

我試過了,但不幸的是,當我去ViewControllerB,然後回到ViewControllerA BOOL仍然是,並沒有提交選擇器。 – rihe 2014-11-15 22:16:13

相關問題