将UINavigationController添加到主视图以外的视图

时间:2012-01-13 19:20:28

标签: iphone uinavigationcontroller

我想在我的应用信息视图中添加UINavigationController(不是我的主视图)。我已经观看/阅读了一些教程,展示了如何使用IB通过AppDelegate将其添加到主窗口。就我而言,我只希望当用户按下信息按钮并将其带到infoView时显示。以下是我在MainViewController中切换到infoView的方法:

- (IBAction)infoButtonPress:(id)sender
{
    // Create pointer to instance of InfoViewController
    InfoViewController *infoView = [[InfoViewController alloc] initWithNibName:@"InfoViewController" bundle:nil];
    // Add view switching animation
    infoView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    // Change view using animation
    [self presentModalViewController:infoView animated:YES];    
}

此时显示infoView,我希望这是UINavigationController的RootView。我尝试添加这一行:

UINavigationController *infoNavController = [[UINavigationController alloc] 
                                             initWithRootViewController:infoView];

创建InfoViewController实例后,应用程序崩溃了。是否可以将UINavigationController添加到主视图以外的视图中?

感谢。

1 个答案:

答案 0 :(得分:1)

您与实施非常接近。按此顺序尝试。

InfoViewController *infoView = [[InfoViewController alloc] initWithNibName:@"InfoViewController" bundle:nil];
UINavigationController *infoNavController = [[UINavigationController alloc] 
                                             initWithRootViewController:infoView];
[infoView release]; // skip this if using ARC
infoView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:infoNavController animated:YES]; 

<强>更新

要杀死此模态,您必须将按钮添加到主模态视图中。

InfoViewController.m

-(void)cancel:(id)sender {
   [self dismissModalViewControllerAnimated:YES];
}

-(void)viewDidLoad {
   [super viewDidLoad];

   UIBarButtonItem *cancelButton =
    [[UIBarButtonItem alloc] initWithTitle: @"Cancel"
                                     style: UIBarButtonItemStylePlain
                                    target: self
                                    action: @selector(cancel:)];
    self.navigationItem.leftBarButtonItem = cancelButton;
    [cancelButton release];
}