在imagePickerController完成后,使用模态呈现一个viewController

时间:2012-12-24 11:09:51

标签: iphone objective-c ios ipad

我需要在从imagePickerController中选择图片后调用一个视图控制器,所以我使用了这段代码:

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

    imageEditorView = [[ImageEditorViewController alloc]initWithNibName:@"ImageEditorViewController" bundle:nil];
    imageEditorView.mainViewController = self;
    imageEditorView.imageBG = [info objectForKey:UIImagePickerControllerOriginalImage];

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

}

但是调试器给了我这个警告:

Warning: Attempt to present <ImageEditorViewController: 0xaa42320> on
     <ViewController: 0x9b58680> whose view is not in the window hierarchy!

并没有礼物!

如果我解决它,那么第一个调试器会给我这个错误:

 Warning: Attempt to present <ImageEditorViewController: 0xa913980> on
 <ViewController: 0x9b332a0> while a presentation is in progress

<!/ P>

已编辑

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil] autorelease];
    } else {
        self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil] autorelease];
    }
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

4 个答案:

答案 0 :(得分:4)

使用以下代码:

[self dismissViewControllerAnimated:YES completion:^{
   [self.parentViewController presentViewController:imageEditorView animated:YES completion:nil];
}];

答案 1 :(得分:1)

<强>更新

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

    imageEditorView = [[ImageEditorViewController alloc]initWithNibName:@"ImageEditorViewController" bundle:nil];
    imageEditorView.mainViewController = self;
    imageEditorView.imageBG = [info objectForKey:UIImagePickerControllerOriginalImage];


    [self performSelector:@selector(presentPicture) withObject:nil afterDelay:2.0];
    [self dismissModalViewControllerAnimated:YES];

}


-(void)presentPicture 
{
     [self presentViewController:imageEditorView animated:YES completion:nil];
}

答案 2 :(得分:0)

首先你必须解雇UIImagePickerController,然后才能使用presentViewController转到另一个视图控制器。

您可以使用以下代码

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

[self dismissModalViewControllerAnimated:YES];
imageEditorView = [[ImageEditorViewController alloc]initWithNibName:@"ImageEditorViewController" bundle:nil];
imageEditorView.mainViewController = self;
imageEditorView.imageBG = [info objectForKey:UIImagePickerControllerOriginalImage];

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

}

答案 3 :(得分:0)

我认为在您的AppDelegate中,您尚未设置rootViewController window。 所以而不是将subview添加到window来制作rootViewController的{​​{1}}。

另一件事可能是您当前的window实际上viewController与另一个subview

我建议将viewController放入self.window.rootViewController = yourMaincontroller