【问题标题】:dismissModelViewControllerAnimated not working for iPhone 5dismissModelViewControllerAnimated 不适用于 iPhone 5
【发布时间】:2013-07-10 04:35:00
【问题描述】:

所以在我的 iPhone 4 设备中,我选择了一张图片后,我希望图片选择器弹出框消失。这适用于 iPhone 4,但以下代码不适用于 iPhone 5。

- (void) loadImage:(UIImage*) image {
float w = image.size.width;
float h = image.size.height;
float maxw = scrollView.frame.size.width;
float maxh = scrollView.frame.size.height;
float wratio = maxw / w;
float hratio = maxh / h;
float ratio = wratio < hratio ? wratio : hratio;
ratio = ratio < 1 ? ratio : 1;

int adjW = (int) (w * ratio);
int adjH = (int) (h * ratio);

UIGraphicsBeginImageContext(CGSizeMake(adjW, adjH));
CGContextRef context = UIGraphicsGetCurrentContext();
[image drawInRect:CGRectMake(0, 0, adjW, adjH)];

CGImageRef scaledImage = CGBitmapContextCreateImage(context);

UIGraphicsEndImageContext();

[appDelegate.model setCurrentImage:[UIImage imageWithCGImage: scaledImage]];
[self clear];
calcButton.enabled = YES;
trashButton.enabled = YES;
scribbleControls.enabled = YES;
[appDelegate.mergeViewCtlr setFirst];
}

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[self loadImage:[info objectForKey:UIImagePickerControllerOriginalImage]];

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ) {
    if ([popoverController isPopoverVisible]) {
        // called for iPad
        [popoverController dismissPopoverAnimated:YES];
    }
}
else {
     // called for iPhone and tried each of the next 3 lines individually
    [self dismissModalViewControllerAnimated:YES]; <== NOT WORKING
    [self dismissViewControllerAnimated:YES completion:nil]; <== ALSO NOT WORKING
    [picker dismissViewControllerAnimated:YES completion:nil]; <== ALSO NOT WORKING
}

[picker release];
}

我还注意到它说 dimissModelViewControllerAnimated 已被弃用,而我应该使用: 改用dismissViewControllerAnimated:completion:。但是我将如何使用它?谢谢

下面是展示模型视图的代码: - (void) actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {

switch (buttonIndex) {
    case 0: { //photo library
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
            UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
            imagePicker.delegate = self;
            imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ) {
                if ([popoverController isPopoverVisible]) {
                    [popoverController dismissPopoverAnimated:YES];
                }
                else {
                    popoverController = [[UIPopoverController alloc] initWithContentViewController:imagePicker];
                    popoverController.delegate = self;
                    [popoverController presentPopoverFromRect:CGRectMake( 250, -50, 320, 480 )
                                                       inView:[self view]
                                     permittedArrowDirections:UIPopoverArrowDirectionUp
                                                     animated:YES];
                }
            }
            else { // for iPhone
                [self presentModalViewController:imagePicker animated:TRUE];
            }
        } else {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Photo library is empty or unavailable" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alertView show];
        }
        break;
    }
    case 1: //camera

【问题讨论】:

  • 您确定正在输入您的 else 块吗?您是否设置了断点以确保它是?如果是这样,那么你应该尝试在选择器上调用dismissViewControllerAnimated:completion: 方法,而不是self。
  • 我确定它正在输入,因为我在上面省略了打印语句。我试过 [picker dismissViewControllerAnimated:YES completion:nil];但它也不起作用。谢谢!

标签: iphone ios ios5 uiviewcontroller uinavigationcontroller


【解决方案1】:

试试这个,

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

并使用

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

您也可以使用respondsToSelector 制作适用于 iOS 5 和 ios6 的案例。

【讨论】:

    【解决方案2】:
    [self dismissViewControllerAnimated:YES completion:nil];
    

    presentModalViewController 和dismissModalViewController 在iOS6 中已弃用

    【讨论】:

      【解决方案3】:

      dismissModalViewControllerAnimated::deprecated in iOS6。请改用dismissViewControllerAnimated:completion:

      【讨论】:

      • 没问题。 presentModalViewController:animated: 也已弃用 - 请尝试改用 presentViewController:animated:completion:
      • 它也不起作用。我不明白为什么只有在我使用照片库后才会出现弹出框。使用相机后它不会停留。
      • 不确定。其他人可能不得不插话。对不起老兄。
      猜你喜欢
      • 1970-01-01
      • 2013-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-03
      • 1970-01-01
      相关资源
      最近更新 更多