iOS:关闭和呈现 ModalViewController,而不访问其父视图控制器



背景:我想驳回我之前提出的模态视图,并立即提出与我刚刚用新信息驳回的相同viewController

问题:如果没有指向以模式呈现第一个ViewController的父ViewController的显式指针,我这样做并不是很成功。我正在尝试编写这个在不弄乱前viewController代码的情况下工作的类。

可能的线索:我一直在尝试几件事:

1.)试图访问父ViewController,目前我不知道该怎么做。

2.) 获得对父级的访问权限后,我可以简单地应用以下代码:

UIViewController* toPresentViewController = [[UIViewController alloc] init];
    [self dismissViewControllerAnimated:YES completion:^{
        [parentViewControllerAccessor presentModalViewController:toPresentViewController animated:YES];
}];

从理论上讲,考虑到可以访问父viewController,这应该可以工作。我对其他方法持开放态度。

假设:您无权更改父ViewController中的任何代码。

你的代码看起来应该可以工作了。如果您使用的是iOS 5,则有一个名为UIViewController属性 presentingViewController

@property(nonatomic, readonly) UIViewController *presentingViewController;

因此,可以使用此属性获取显示模式控制器的视图控制器。

注意:在iOS 4中,parentViewController将设置为演示控制器,因此,如果您同时支持iOS 4和5,则必须首先检查操作系统版本以决定要访问的属性。在 iOS 5 中,Apple 已修复此问题,因此parentViewController现在专门用于包含视图控制器的父级(请参阅UIViewController文档中有关实现容器视图控制器的部分)。

编辑:关于从块内访问self.presentingViewController:在调用块时(在模式视图控制器关闭后),presentingViewController属性可能会设置为 nil。请记住,块内的self.presentingViewController在执行块时给出属性的值,而不是在创建块时给出属性的值。要防止这种情况,请执行以下操作:

UIViewController* toPresentViewController = [[UIViewController alloc] init];
UIViewController* presentingViewController = self.presentingViewController;
[self dismissViewControllerAnimated:YES completion:^
{
    [presentingViewController presentModalViewController:toPresentViewController animated:YES];
}];

这是必要的,不是因为self消失/消除(它被块安全地保留),而是因为它不再存在,因此它的presentingViewController现在为零。没有必要将presentingViewController存储在其他任何地方,局部变量很好,因为它将被块保留。

您可以使用通知来完成此操作。

例如,当您希望将其关闭时,从模式视图外部触发此通知:

[[NSNotificationCenter defaultCenter] postNotificationName:@"dismissModalView" 
                                                    object:nil 
                                                  userInfo:nil];

然后在模态视图中处理该通知:

- (void)viewDidLoad {
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(dismissMe:)
                                                 name:@"dismissModalView" 
                                               object:nil];
}
- (void)dismissMe:(NSNotification)notification {
    // dismiss it here.
}

iOS5的解决方案:

-(void)didDismissModalView:(id)sender {
   // Dismiss the modal view controller
   int sold=0;
   if(sold==0){
      //Cash_sold.delegate = self;
      // Cash_sold.user_amount.text=[NSString stringWithFormat:@"%d",somme];
      Cash_sold = [[CashSoldview alloc] initWithNibName:@"CashSoldview" bundle:nil];
      CGRect fram1 = CGRectMake(200,20,400,400);
      Cash_sold.view.superview.frame = fram1;
      Cash_sold.view.frame=fram1;
      Cash_sold.modalTransitionStyle= UIModalTransitionStyleCoverVertical;
      Cash_sold.modalPresentationStyle=UIModalPresentationFormSheet;
      UIViewController* presentingViewController = self.parentViewController;
      [self dismissViewControllerAnimated:YES completion:^
      {
         [presentingViewController presentModalViewController:Cash_sold animated:YES];
      }];     
   }
}

尝试以下代码:

[self dismissViewControllerAnimated:NO 
                         completion:^{
  // instantiate and initialize the new controller
  MyViewController *newViewController = [[MyViewController alloc] init];
  [[self presentingViewController] presentViewController:newViewController
                                                animated:NO
                                              completion:nil];
}];

最新更新