iOS:解除并呈现ModalViewController而无需访问其Parent ViewController

时间:2012-06-11 20:29:39

标签: iphone ios ipad modalviewcontroller presentmodalviewcontroller

背景:我想解雇我之前提到的modalView,并立即呈现我刚刚用新信息解雇的viewController

问题:如果没有明确指向第一个ViewController模式的父ViewController的指针,我就不会非常成功。我正在尝试编写这个有用的代码,而不会弄乱前面的viewController代码。

可能的领导:我一直在尝试一些事情:

1。)试图访问父ViewController,这时我不知道如何。

2.。)一旦获得父母的访问权限,我只需应用以下代码:

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

理论上,这应该适用于父viewController的访问权限。我对其他方式持开放态度。

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

4 个答案:

答案 0 :(得分:11)

您的代码看起来应该可以正常工作。如果您使用的是iOS 5,则会有一个名为UIViewController的{​​{1}}属性。

presentingViewController

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

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

编辑:关于从块中访问UIViewController:在调用块时(在模式视图控制器被解除之后),self.presentingViewController属性可能会被设置没有。请记住,块中的presentingViewController在执行块时提供属性的值,而不是在创建块时。为防止这种情况,请执行以下操作:

self.presentingViewController

这不是必要的,因为UIViewController* toPresentViewController = [[UIViewController alloc] init]; UIViewController* presentingViewController = self.presentingViewController; [self dismissViewControllerAnimated:YES completion:^ { [presentingViewController presentModalViewController:toPresentViewController animated:YES]; }]; 已经消失/被解除(它被块安全保留),但因为它不再呈现,因此它的self现在为零。没有必要将presentingViewController存储在其他地方,局部变量很好,因为它将被块保留。

答案 1 :(得分: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.
}

答案 2 :(得分: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];
      }];     
   }
}

答案 3 :(得分:1)

请尝试以下代码:

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