web-dev-qa-db-fra.com

Swift - Instanciation d'un contrôleur de navigation sans storyboards dans App Delegate

Je suis en train de reconstruire une application sans story-boards et la partie avec laquelle j'ai le plus de difficulté est la navigation par programme. Peu de choses sont écrites là-bas qui n'utilisent pas de story-boards, donc trouver une réponse à cela a été difficile.

Mon problème est assez simple. J'ai mon ViewController et mon SecondViewController et je veux pousser de l'ancien vers le second.

Dans 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
}

Puis dans 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)
    }
}

L'impression self.navigationController Renvoie zéro. J'ai essayé de faire:

var navController = UINavigationController() lorsque la classe ViewController est créée (mais en dehors de ViewDidLoad, juste sous la déclaration de classe) et fait le Push en utilisant le navController var mais cela n'a pas fonctionné.

Je pense que la solution est peut-être de créer un contrôleur de navigation dans App Delegate que toute l'application utiliserait, je suppose comme variable globale?

J'espère que ce message peut servir à beaucoup d'autres qui sont nouveaux à Swift et veulent supprimer les storyboards de leur application.

Merci d'avoir regardé et de votre aide.

25
Zack Shapiro

Dans Swift

Placez ce code dans la méthode didFinishLaunchingWithOptions de la classe AppDelegate.

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()
36
jaiswal Rajan

Dans 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
}

Dans 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.
}


}
18
agy