2014-12-13 66 views
0

目前我使用StoryBoard为iPad开发。另外我添加一个ViewController.xib文件和一个视图。现在我想把按钮从xib返回到故事板。我的代码在下面,但这不起作用。从xib到故事板的后退按钮

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

你如何展示这个视图控制器?从故事板场景? – bl4stwave 2014-12-13 07:20:44

+0

故事板到xib我正在使用下面的代码。 UITableViewCell * selectedCell = [tableView cellForRowAtIndexPath:indexPath]; NSString * cellText = selectedCell.detailTextLabel.text; NSLog(@“item title:%@”,cellText); ViewController * vc = [[ViewController alloc] initWithNibName:@“ViewController”bundle:nil]; self.window.rootViewController = self.viewController; vc.strFullName = cellText; [self.view addSubview:vc.view]; – Saravanan 2014-12-13 07:34:10

+0

我希望我的回答可以帮助您 – bl4stwave 2014-12-13 07:46:22

回答

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]; 
} 
+0

谢谢这段代码正在工作 – Saravanan 2014-12-13 15:38:35

+0

@Saravanan,不客气!顺便说一下,如果您发现它有助于您的情况,您可以接受此答案。 – bl4stwave 2014-12-13 16:51:10

+0

当然。感谢stwave – Saravanan 2014-12-15 04:37:23

1

忘掉navigationController

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

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

并解雇。

[self dismissViewControllerAnimated:self completion:nil];