2016-08-26 43 views
0

我正嘗試在新文件中創建一個新的UIViewController類,並從默認的ViewController轉換爲UIViewController。在Objective-C中,我會創建一個新文件以及相應的xib文件,然後將該文件導入我的主ViewController.m,並使用視圖的presentViewController方法轉換到新視圖。創建並轉換視圖而不使用故事板swift

示例:從主ViewController.m

SecondViewController* secondView = [[SecondViewController alloc] initWithNibName:@"SecondViewControllerXib" bundle:nil]; 
[self presentViewController:secondView animated:YES completion:nil]; 
secondView = nil; 

然後返回到主ViewController.m使用

[self dismissViewControllerAnimated:YES completion:nil]; 

我已經四處搜尋,但所有的例子我已經遇到均使用Storyboard我不喜歡使用。如何在不使用快速使用Storyboard的情況下實現這一目標?

回答

0

在使用Storyboard和.XIB文件或在純代碼中執行所有UIKit方面沒有什麼不同。 Swift和Obj-C可以在這方面使用相同的IDE工具,因此您可能正在尋找一個不存在的問題。

好像你只是問如何編寫基於現有Obj-C代碼的Swift語法。 「編程視圖控制器示例與斯威夫特」應該可以多一點努力,但對你的代碼:

import UIKit 

class SecondViewController: UIViewController { 
} 

class FirstViewController: UIViewController { 
    func foo() { 
     let secondView = SecondViewController(nibName: "SecondViewControllerXib", bundle: nil) 
     presentViewController(secondView, animated: true, completion: nil) 
    } 
    func bar() { 
     dismissViewControllerAnimated(true, completion: nil) 
    } 
} 
+0

謝謝,我想我是過度想象和困惑自己。 – RickyTheCoder

相關問題