Home>

I'm writing code to make screen transitions with swift, but it doesn't work.

Function to be implemented: Tap the button located to the right of the navigation bar to make a push transition.

Result: No error occurs when executed, but no transition occurs when tapped. When you tap it, the debug print statement in the button's function is executed, so you can create the button itself.

swift This is a poor code for beginners, but I would appreciate your favor.

First screen
import UIKit
class ViewController: UIViewController {

    let navBar = UINavigationBar (frame: CGRect (x: 0, y: 40, width: UIScreen.main.bounds.size.width, height: 100))
    let navItem: UINavigationItem = UINavigationItem (title: "title")
    override func viewDidLoad () {

 super.viewDidLoad ()
        view.backgroundColor = UIColor.gray
        navItem.rightBarButtonItem = UIBarButtonItem (title: "Transition", style: UIBarButtonItem.Style.plain, target: self, action: #selector (self.rightHandAction))
        navBar.pushItem (navItem, animated: true)
        self.view.addSubview (navBar)

    @objc func rightHandAction (_ sender: UIBarButtonItem) {
        print ("right bar button action")
        let vcC: UIViewController = viewControllerConfig ()
        let naviVC: UINavigationController = UINavigationController (rootViewController: vcC)
        self.navigationController? .pushViewController (naviVC, animated: true)
    }
}
Transition destination screen
import UIKit
class viewControllerConfig: UIViewController {
    override func viewDidLoad () {
        super.viewDidLoad ()
        self.title = "Config"
        self.view.backgroundColor = UIColor.cyan
    }
}

`` `

  • Answer # 1

    self.navigationController? .pushViewController (naviVC, animated: true) HerenavigationController I feel that is nil.

    Try the following as a trial. I think the contents of print will be output.

    guard let naviCon = self.navigationController else {
      print ("navigationController is nil!")
      return
    }

    How to add UINavigationController (Working with Storyboard)

    Add UINavigationController to Storyboard

    Specify ViewController in RootViewController of UINavigationController

    Select UINavigationController and check Is Initial View Controller of the image

      

    with thisself.navigationController Will no longer be nil.
    I think that it is necessary to correct the processing around this after adding it.

    navItem.rightBarButtonItem = UIBarButtonItem (title: "Transition", style: UIBarButtonItem.Style.plain, target: self, action: #selector (self.rightHandAction))
            navBar.pushItem (navItem, animated: true)
            self.view.addSubview (navBar)

    I think that I could not push from NavigationController to NavigationController, so I think that it also needs to be fixed here.

    let vcC: UIViewController = viewControllerConfig ()
            let naviVC: UINavigationController = UINavigationController (rootViewController: vcC)
            self.navigationController? .pushViewController (naviVC, animated: true)