未调用ParentViewController的ViewWillAppear

时间:2012-12-12 09:46:56

标签: objective-c ios ios5 viewwillappear

我正在尝试实现自定义UIActionSheet(由ViewController组成) 我已将View Controller作为subView添加到我的rootView的导航控制器

- (IBAction)ShowMenu:(id)sender
{
   [self.navigationController.view addSubview:self.menuViewController.view];
   [self.menuViewController setTest:YES];
   [self.menuViewController viewWillAppear:YES];
}

这里的MenuViewController有一个tableview,几乎没有选择的选项。选择后我打开那些相应的ViewControllers。假设我点击了menu1,然后打开menu1ViewController,它运行正常。现在当我关闭这个viewController时,我正在调用dismissViewController。

并且在menuViewController中我已经编写了通过menuviewController动画到底部的代码,它工作正常。

但是MenuView的父级是TestViewController,当menuviewController动画化时,不会调用函数viewdidAppear。

这就是我的问题,

我正在使用此代码通过menuViewController动画到底部

- (void) slideOut {


  [UIView beginAnimations:@"removeFromSuperviewWithAnimation" context:nil];

    // Set delegate and selector to remove from superview when animation completes
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];

    // Move this view to bottom of superview
    CGRect frame = self.menusheet.frame;
    frame.origin = CGPointMake(0.0, self.view.bounds.size.height);
    self.menusheet.frame = frame;

    [UIView commitAnimations];
}

// Method called when removeFromSuperviewWithAnimation's animation completes
- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
    if ([animationID isEqualToString:@"removeFromSuperviewWithAnimation"]) {
        [self.view removeFromSuperview];
    }
}

MenuViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    if(m_test)
    {
        [self slideIn];
        m_test = FALSE;
    }
    else
    {
        [self slideOut];       
    }

}

1 个答案:

答案 0 :(得分:3)

恕我直言,-[UIViewController viewWillAppear]-[UIViewController viewDidAppear]只会被称为容器式控制器(如导航控制器,标签栏控制器)将被调用者添加到视图控制器层次结构中。

如果您只是通过在代码中调用addSubview:来添加视图,则不会调用它。 See also

您可以在代码中,在您使用或不使用动画调用-viewWillAppear之前和之后,以编程方式拨打-viewDidAppearaddSubview: