在iOS6中不推荐使用presentModalViewController:Animated


101

我将以下代码用于图像选择器。但是,当我在模拟器中运行它时,内存泄漏,并且收到有关presentModalViewcontroller:animated在iOS6中弃用的警告。我也被dismissModalViewController:animated弃用了。我正在使用SDK 6.1。

ImagePicker的代码:

- (void)showAlbum:(id)sender { 
    imagePicker=[[UIImagePickerController alloc]init];
    imagePicker.delegate = self;
    imagePicker.allowsEditing =NO;
    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    [self presentModalViewController:imagePicker animated:YES];
}

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

Answers:


216

使用此行并检查:

[self presentViewController:imagePicker animated:YES completion:nil];

1
代替此:[self presentModalViewController:imagePicker动画:是];
Vishal

8
对于关闭,请使用以下命令:[self dismissViewControllerAnimated:YEScomplete:nil];
维沙尔

获取相同的内存泄漏问题,应用程序将关闭
Ram

问题出在哪里意味着哪条线?
Vishal

我收到此错误“ UIApplicationInvalidInterfaceOrientation”,原因:“ preferredInterfaceOrientationForPresentation必须返回支持的接口方向!”
拉姆

17
[[Picker presentingViewController] dismissViewControllerAnimated:YES completion:nil];

代替

 [[Picker parentViewControl] dismissModalViewControllerAnimated:YES];

[self presentViewController:picker animated:YES completion:nil];

代替

[self presentModalViewController:picker animated:YES];

2
所以现在我们有了presentViewController,并且不指定视图控制器应该是模态的?
Septiadi Agus

4

正如维沙尔所说

[self presentViewController:imagePicker animated:YES completion:nil];
[self dismissViewControllerAnimated:YES completion:nil];

确保您还添加了“ completion:nil”


4
if ([self respondsToSelector:@selector(presentViewController:animated:completion:)])
{
    [self presentViewController:objSignupViewController animated:^{} completion:nil];
}
else
{
    [self presentModalViewController:objSignupViewController animated:YES];
}

2

用:

[self presentViewController:imagePicker animated:YES completion:nil];

然后用于解雇模态:

[self dismissViewControllerAnimated:controller completion:nil];

要么

[self dismissViewControllerAnimated:YES completion:nil];
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.