在解除模态视图控制器后呈现不同的视图控制器

时间:2012-06-27 12:58:22

标签: objective-c ios uiviewcontroller

我为iOS应用中的各种UI设置提供了一个模态视图控制器。其中一个设置允许用户选择不同的主视图。当他们点击“完成”时,我想关闭模态视图并显示新选择的视图控制器,而没有旧视图控制器分割到新视图控制器的瞬间延迟。怎么可以实现呢?

更新

这是我使用Eugene技术成功实现的方法,但没有app委托。相反,此实现特定于我的场景,其中导航堆栈中的视图控制器在实用程序应用程序中显示模态视图控制器。

- (void)swapFrontSideViewController;
{
    UINavigationController *navigationVC = (UINavigationController *)[self presentingViewController];
    NSMutableArray *viewControllers      = [NSMutableArray arrayWithArray:navigationVC.viewControllers];
    UIViewControllerSubclass *selectedViewController = nil;
    if ([self.selectedFrontSide isEqualToString:FRONT_SIDE_NAME1]) {
        selectedViewController = [self.storyboard instantiateViewControllerWithIdentifier:FRONT_SIDE_NAME1];
    } else if ([self.selectedFrontSide isEqualToString:FRONT_SIDE_NAME2]) {
        selectedViewController = [self.storyboard instantiateViewControllerWithIdentifier:FRONT_SIDE_NAME2];
    }
    if (selectedViewController) {
        [viewControllers replaceObjectAtIndex:viewControllers.count -1 withObject:selectedViewController];
        [navigationVC setViewControllers:viewControllers];
        self.delegate = selectedViewController;
    } else {
        NSLog(@"Error: Undefined Front Side Selected.");
    }
}

1 个答案:

答案 0 :(得分:2)

- (IBAction)doDismiss:(id)sender {
  AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate; // Get the app delegate
  NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:appDelegate.navigationController.viewControllers]; // fetch its navigationController viewControllers stack
  UIViewController *replacementController; //initialize replacement controller
  [viewControllers replaceObjectAtIndex:viewControllers.count -1 withObject:replacementController]; // replace the top view controller in stack with the replacement one
  [appDelegate.navigationController setViewControllers:viewControllers]; //change the stack
  [self dismissModalViewControllerAnimated:YES];
}