从xib到故事板的后退按钮

时间:2014-12-13 07:03:44

标签: ios objective-c iphone xcode

目前我开发了一款适用于iPad的StoryBoard。另外,我添加了一个带有视图的ViewController.xib文件。现在我想从xib到storyboard返回按钮。我的代码如下,但这不起作用。

UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *initViewController = [storyBoard instantiateInitialViewController];
[self.navigationController pushViewController:initViewController animated:YES];

2 个答案:

答案 0 :(得分:3)

您可以使用故事板场景呈现视图控制器 - presentViewController:animated:completion:(例如,对于模态方案)

ViewController *vc= [[ViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:vc];
[self presentViewController:navController animated:YES completion:NULL];

然后,您可以添加自定义后退按钮

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Back", @"Back from ViewController.XIB") style:UIBarButtonItemStylePlain target:self action:@selector(backAction:)];
self.navigationItem.leftBarButtonItem = backButton;

并驳回它:

- (void)backAction:(id)sender{
    [self.presentingViewController dismissViewControllerAnimated:YES completion:NULL];
}

答案 1 :(得分:1)

忘掉navigationController

在您的活动视图控制器中,像这样初始化新的视图控制器。

UIViewController *initViewController = [[UIViewController alloc] 
    initWithNibName:@"YourNibFile" bundle:[NSBundle mainBundle]];
[self presentViewController:initViewController animated:YES completion:nil];

并解雇。

[self dismissViewControllerAnimated:self completion:nil];