setNavigationBarHidden:在removeFromSuperview之后没有工作

时间:2011-05-15 08:33:11

标签: iphone objective-c xcode ios

我在我的Detail View Controller顶部添加了一个UIWebView子视图,它有一个导航栏。我想在WebView子视图中隐藏导航栏,并在从superview中删除时再次显示它,以便在查看WebView时有更多的屏幕空间。

我的代码的问题是添加子视图后导航栏被成功隐藏,但是当删除子视图时尝试再次显示导航栏时它不起作用。

非常感谢任何帮助。谢谢。

这是我的代码:

// In InstrumentsDetailViewController.m

- (IBAction)edu1Link:(id)sender {

    _webViewController = [[WebViewController alloc]
                          initWithNibName:@"WebViewController" bundle:nil];

    [UIView beginAnimations:@"flipping view" context:nil];
    [UIView setAnimationDuration:1];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft
                           forView:self.view cache:YES];

    [self.view addSubview:_webViewController.view];

    [self.navigationController setNavigationBarHidden:YES animated:YES];

    [UIView commitAnimations];
}


// In WebViewController.m

- (IBAction) doneButton:(id)sender {

    [UIView beginAnimations:@"flipping view" context:nil];
    [UIView setAnimationDuration:1];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationTransition: UIViewAnimationTransitionFlipFromRight
                           forView:self.view.superview cache:YES];

    [self.view removeFromSuperview];

    [self.navigationController setNavigationBarHidden:NO animated:YES];

    [UIView commitAnimations];
}

1 个答案:

答案 0 :(得分:1)

我在代码中看到的内容: - 您正在向InstrumentDetailViewController实例主视图添加子视图。 - 子视图是WebViewController实例的主视图。

但是...... WebViewController永远不会被推送或弹出到您的导航堆栈。因此,您的WebViewController实例没有对navigationController的引用,并且调用[self.navigationController]会将您的setNavigationBarHidden:NO animated:YES消息发送给... nil

您可以: - 将WebviewController实例推送/弹出到导航堆栈,因此它将引用navigationController。 要么 - 将您的WebViewController实例添加为InstrumentsDetailViewController的CHILD,然后调用

[[[self parentViewController] navigationController] setNavigationBarHidden:NO animated:YES];

http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html

搜索addChildViewController:

相关问题