2015-06-20 61 views
18

我重建而不故事板的应用程序,并且我具有最麻烦被编程导航视图到视图中的一部分。很少有东西写在那里不使用故事板,所以找到答案是困难的。夫特 - 实例化而不故事板导航控制器中应用代表

我的问题很简单。我有我的ViewController和我的SecondViewController,我想从前者推到后者。

AppDelegate

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 
    // Override point for customization after application launch. 

    window = UIWindow(frame: UIScreen.mainScreen().bounds) 
    window?.backgroundColor = UIColor.whiteColor() 
    window?.rootViewController = ViewController() 
    window?.makeKeyAndVisible() 

    return true 
} 

然后在ViewController.swift

class ViewController: UIViewController, AVAudioPlayerDelegate, UITextFieldDelegate { 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     startFinishButton.setTitle("Begin", forState: .Normal) 
     startFinishButton.addTarget(self, action: "moveToSecondViewController", forControlEvents: .TouchUpInside) 
     view.addSubview <*> startFinishButton 
    } 

    func moveToSecondViewController(sender: UIButton) { 
     let vc = SecondViewController() 
     println(self.navigationController) // returns nil 
     self.navigationController?.pushViewController(vc, animated: true) 
    } 
} 

印刷self.navigationController返回nil。我试着这样做:

var navController = UINavigationController()ViewController类创建(但viewDidLoad中之外,对下类声明),并使用navController VAR进行推送,但是这并没有奏效。

我想也许该解决方案是在应用程序委托,以创建导航控制器,我想整个应用程序将使用一个全局变量?

我希望这篇文章可以成为许多人谁是新的雨燕,并希望从他们的应用程序中删除故事板。

感谢您的关注和帮助。

回答

20

在夫特3

将此代码放入AppDelegate类中的didFinishLaunchingWithOptions方法内。

window = UIWindow(frame: UIScreen.main.bounds) 
let mainController = MainViewController() as UIViewController 
let navigationController = UINavigationController(rootViewController: mainController) 
navigationController.navigationBar.isTranslucent = false 
self.window?.rootViewController = navigationController 
self.window?.makeKeyAndVisible() 
+0

你有什么自定义的navigationController? – user7097242

16

在AppDelegate中

var window: UIWindow? 
var navController: UINavigationController? 

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 

    navController = UINavigationController() 
    var viewController: ViewController = ViewController() 
    self.navController!.pushViewController(viewController, animated: false) 

    self.window = UIWindow(frame: UIScreen.mainScreen().bounds) 

    self.window!.rootViewController = navController 

    self.window!.backgroundColor = UIColor.whiteColor() 

    self.window!.makeKeyAndVisible() 

    return true 
} 

在的ViewController

class ViewController: UIViewController { 

override func viewDidLoad() { 
    super.viewDidLoad() 
    self.title = "FirstVC" 

    var startFinishButton = UIButton.buttonWithType(UIButtonType.System) as! UIButton 
    startFinishButton.frame = CGRectMake(100, 100, 100, 50) 
    startFinishButton.backgroundColor = UIColor.greenColor() 
    startFinishButton.setTitle("Test Button", forState: UIControlState.Normal) 
    startFinishButton.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside) 

    self.view.addSubview(startFinishButton) 
} 

func buttonAction(sender:UIButton!) 
{ 
    println("Button tapped") 
    let vc = SecondViewController() 
    self.navigationController?.pushViewController(vc, animated: true) 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 


} 
+0

为什么你将viewcontroller设置为uiviewcontroller而不是我的第一个视图控制器ViewController.swift? –

+0

@ZackShapiro,我编辑了我的答案。我用这段代码在Xcode中创建了一个例子,它可以工作。 – agy

+0

谢谢,当我回到我的机器时会试一试 –

相关问题