6

背景:我想关闭我之前提出的模态视图,并立即展示viewController我刚刚用新信息解除的相同视图。

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

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

1.)试图访问 parent ViewController,此时我不知道如何。

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

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

从理论上讲,这应该可以访问 parent viewController。我对其他方法持开放态度。

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

4

4 回答 4

10

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

@property(nonatomic, readonly) UIViewController *presentingViewController;

因此,您可以使用此属性来获取呈现您的模态控制器的视图控制器。

注意:在 iOS 4parentViewController中将设置为呈现控制器,因此如果您同时支持 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其他任何地方存储,局部变量很好,因为它将被块保留。

于 2012-06-11T21:00:11.713 回答
1

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

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

[[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.
}
于 2012-06-11T20:59:31.620 回答
1

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];
      }];     
   }
}
于 2012-07-26T13:51:53.270 回答
1

试试下面的代码:

[self dismissViewControllerAnimated:NO 
                         completion:^{
  // instantiate and initialize the new controller
  MyViewController *newViewController = [[MyViewController alloc] init];
  [[self presentingViewController] presentViewController:newViewController
                                                animated:NO
                                              completion:nil];
}];
于 2012-07-26T14:32:16.097 回答