如何在 Swift 中以编程方式创建 SplitViewController?
Posted
技术标签:
【中文标题】如何在 Swift 中以编程方式创建 SplitViewController?【英文标题】:How can create a SplitViewController programmatically in Swift? 【发布时间】:2014-06-10 07:46:45 【问题描述】:我需要执行哪些步骤?在 Objective-C 中,我们创建了 rootViewController
和 detailViewController
,之后将这些控制器添加到 splitViewController
。例如:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
UISplitViewController *splitViewController = [[UISplitViewController alloc] init];
MTTRootViewController *rootViewController = [[MTTRootViewController alloc] init];
MTTDetailedViewController *detailedViewController = [[MTTDetailedViewController alloc]init];
splitViewController.viewControllers = [NSArray arrayWithObjects:rootViewController, detailedViewController, nil];
[self.window setRootViewController:(UIViewController*)splitViewController];
[self.window makeKeyAndVisible];
return YES;
如何在 Swift 中做同样的事情?
【问题讨论】:
【参考方案1】:如果你想用 navigationController 来做,那就试试吧:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window!.backgroundColor = UIColor.whiteColor()
var splitViewController = UISplitViewController()
var rootViewController = RootViewController()
var detailViewController = DetailViewController()
var rootNavigationController = UINavigationController(rootViewController:rootViewController)
var detailNavigationController = UINavigationController(rootViewController:detailViewController)
splitViewController.viewControllers = [rootNavigationController,detailNavigationController]
self.window!.rootViewController = splitViewController
self.window!.makeKeyAndVisible()
return true
【讨论】:
【参考方案2】:一段时间后我找到了答案:
第一次需要创建rootViewController和detailViewController。例如,rootViewController 将继承自 UITableViewController,detailViewController 将继承自 UIViewController。 下次你需要这样做:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window!.backgroundColor = UIColor.whiteColor()
var splitViewController = UISplitViewController()
var rootViewController = RootViewController()
var detailViewController = DetailViewController()
splitViewController.viewControllers = [rootViewController,detailViewController]
self.window!.rootViewController = splitViewController
self.window!.makeKeyAndVisible()
return true
【讨论】:
以上是关于如何在 Swift 中以编程方式创建 SplitViewController?的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Swift 中以编程方式创建新的 UIViewController
如何在ios swift中以编程方式创建自动布局等宽度约束?
如何使用 Swift 在 iOS 中以编程方式进行 segue