簡體   English   中英

如何在故事板中創建UIViewController布局,然后在代碼中使用它?

[英]How to create a UIViewController layout in storyboard and then use it in code?

我的iOS 5應用程序中有一個Storyboard。

在那里,我創建了許多屏幕,它完美地運作。

但是,我在代碼中創建了一個視圖控制器,而不是UI操作的結果,而是處理數據的結尾。 我想顯示這個視圖控制器,然后,作為modalViewController,但也有它在故事板編輯器中設計。

可能嗎? 使用筆尖我這樣做:

ResultsController *rc = [[ResultsController alloc] initWithNibName:@"ResultsController"
                                                            bundle:nil];
[self.navigationController presentModalViewController:rc animated:YES];
[rc release];

現在我真的沒有nib文件,所以我該怎么做?

看一下UIStoryboard類。 有一個instantiateViewControllerWithIdentifier方法。 因此,您需要在StoryController中為ResultsController ViewController設置Identfier。

你可以做這樣的事情

UIViewController *viewController = 
   [[UIStoryboard storyboardWithName:@"MainStoryboard" 
                              bundle:NULL] instantiateViewControllerWithIdentifier:@"ResultsController"];

[self presentModalViewController:viewController animated:NO];

在你的故事板中:

  1. 添加一個通用的UIViewController。
  2. 使用Identity Inspector,將其自定義類設置為ResultsController。
  3. 從源視圖控制器到ResultsController創建模態segue

對於Swift 4

let viewController = UIStoryboard.init(name: "MainStoryboard", bundle: nil).instantiateViewController(withIdentifier: "ResultsController")
self.present(viewController, animated: true, completion: nil)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM