0

I'm working on data passing from one view to another in Swift. I'm using the following code:

@IBAction func loginAction(sender: AnyObject) {
let nextViewController = NextViewController(nibName: "NextViewController", bundle: nil) 
self.navigationController?.pushViewController(nextViewController, animated: true) 
}

However it will not navigate second page, why?

Ronan Boiteau
  • 9,608
  • 6
  • 34
  • 56
nandu kathmandi
  • 33
  • 1
  • 12

1 Answers1

0

If you are not using StoryBoard, you can use the following code: AppDelegate.swift

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    var userInputViewContrller: UserInputViewController = UserInputViewController()
    var navigationViewController: UINavigationController = UINavigationController()


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

        self.window = UIWindow.init(frame: UIScreen.mainScreen().bounds)
        self.window?.backgroundColor = UIColor.whiteColor()
        self.navigationViewController = UINavigationController.init(rootViewController: userInputViewContrller)
        self.window?.rootViewController = self.navigationViewController
        self.window?.makeKeyAndVisible()

        return true
    }

Now you have set the rootViewController for window as your naviagationViewController which is initialised with variable for the page "userInputViewContrller"

import UIKit

class UserInputViewController: UIViewController {

    override func viewWillAppear(animated: Bool) {

        let viewFrame = CGRect(x: 1, y: 30, width: 373, height: 635)
        let view: UIView = UIView(frame: viewFrame)
        self.view.addSubview(view)

        let clickMeButton:UIButton = UIButton()
        let nextButtonFrame = CGRect(x: 200, y: 275, width: 150, height: 30)
        nextButton = UIButton(frame: nextButtonFrame)
        clickMeButton.setTitle("Click here", forState: UIControlState.Normal)
        clickMeButton.addTarget(self, action: "clickMeTapped:", forControlEvents: UIControlEvents.TouchUpInside)
        view.addSubview(clickMeButton)
    }

    func clickMeTapped(sender: UIButton){
        let itemViewController = ItemViewController()
        self.navigationController?.pushViewController(itemViewController, animated: true)
    }
}

On click of "Click here" button in UserInputViewController you will be navigated to the next screen itemViewController.

import UIKit

class ItemViewController: UIViewController {
    override func viewWillAppear(animated: Bool) {
        //View

        let viewFrame = CGRect(x: 1, y: 30, width: 373, height: 635)
        let view: UIView = UIView(frame: viewFrame)
        self.view.addSubview(view)

        //Header Label
        let headerLabelFrame = CGRect(x: 25, y: 50, width: 325, height: 30)
        var headerLabel:UILabel = UILabel()
        headerLabel = UILabel(frame: headerLabelFrame)
        headerLabel.text = "Hello I'm in itemViewVireControllr"
        view.addSubview(headerLabel)
  }
}
Anitha
  • 129
  • 1
  • 8