2014-12-13 61 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];