【问题标题】:RootView navigation in swiftSwift 中的 RootView 导航
【发布时间】:2014-11-11 10:04:52
【问题描述】:
我是一名 Objectie-C 开发人员。对于根视图导航,我在 Objectie-c 中使用了以下代码
FirstViewController *fvc=[[FirstViewController alloc]init];
UINavigationController *nv=[[UINavigationController alloc]initWithRootViewController:fvc];
self.window.rootViewController=nv;
对于我的新项目,我正在 swift 中制作它。我只想从 AppDelegate.swift 制作 RootView 导航。
【问题讨论】:
标签:
ios
swift
navigation
ios8
【解决方案1】:
检查下面的代码。首先我们创建窗口。然后分配初始化视图控制器。然后用 rootcontroller 作为 viewcontroller 分配导航控制器。和 Window 的根控制器作为导航控制器。
var window: UIWindow?
var navC : UINavigationController?
var vc:ViewController?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
{
// alloc init window
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
// view controller
self.vc = ViewController(nibName: "ViewController", bundle: nil);
// create navigation controller with root = vc.
self.navC = UINavigationController(rootViewController: self.vc!);
self.navC?.navigationBar.hidden = true;
// window's root controller as navigation controller.
self.window?.rootViewController = self.navC
self.window?.makeKeyAndVisible()
return true
}
也许这会对你有所帮助。
【解决方案2】:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
var rootVie: FirstViewController = FirstViewController() // this is allocation method in swift
if let window = self.window{
window.rootViewController = rootVie
}
return true
}
需要参考使用这个link