2013-05-10 34 views
3

我已經嘗試了任何運氣的解僱UIImagePickerController的每一個變化。我究竟做錯了什麼。關閉UIImagePickerController

- (IBAction)choosePhoto 
{ 
    self.picker = [[UIImagePickerController alloc] init]; 
    self.picker.delegate = self; 
    self.picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 
    [self presentModalViewController:self.picker animated:YES]; 

} 

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)imagePicker 
{ 
    NSLog(@"dismiss image picker"); 
    [self dismissModalViewControllerAnimated:NO]; 
    [[self.picker parentViewController] dismissModalViewControllerAnimated:NO]; 
    [self.presentedViewController dismissModalViewControllerAnimated:NO]; 
    [self.presentingViewController dismissModalViewControllerAnimated:NO]; 
    // And every other way i could think of 
} 

- (void)imagePickerController:(UIImagePickerController *)imagePicker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
    .. same stuff here 
} 

我試圖從父母,祖父母目前的選擇器,navigationController和根控制器和沒有什麼作品。我永遠不會解僱ImagePickerController。

請注意每次都會調用日誌語句。

乾杯

+2

有你嘗試[拾取dismissViewControllerAnimated:YES完成:零] ? – Ushan87 2013-05-10 05:10:24

+0

不行,不行。我應該添加即時通訊使用故事板和弧 – user346443 2013-05-10 05:14:28

+1

你有雙重檢查,UIImagePickerControllerDelegate,UINavigationControllerDelegate設置在你的.h文件? – Ushan87 2013-05-10 05:15:25

回答

9

試試這條線。它可能適合你。

[self.picker dismissModalViewControllerAnimated:NO]; 

而對於iOS 6的,後來用這個

[self.picker dismissViewControllerAnimated:NO completion:nil]; 

還可以使用此代碼來展示你的選擇器控制器

if ([self respondsToSelector:@selector(presentViewController:animated:completion:)]){ 
    [self presentViewController:self.picker animated:YES completion:nil]; 
} else { 
    //To target iOS 5.0 
    [self presentModalViewController:self.picker animated:YES]; 
} 
6

您是否正在運行iOS 6?如果是這樣,presentModalViewController:已棄用,可能會導致一些意外的結果。改爲使用presentViewController:animated:completion:

但在技術上,這裏就是你應該做的:

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)imagePicker 
{ 
    [imagePicker dismissViewControllerAnimated:NO completion:nil];//Or call YES if you want the nice dismissal animation 
} 
+0

簡短有效。 – 2014-05-22 15:26:42

3

對於SWIFT使用本...

func imagePickerControllerDidCancel(picker: UIImagePickerController!) 
{ 
    picker.dismissViewControllerAnimated(true, completion: nil) 
} 
1

對於斯威夫特4:

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) { 
     picker.dismiss(animated: true, completion: nil) 
}