4

When I call navigation controller from view controller, navigation controller become nil. How can I access navigation controller?

override func setUp() {
    self.viewController = storyboard.instantiateViewControllerWithIdentifier("FirstViewController") as! FirstViewController
    self.viewController.loadView()
    XCTAssertNotNil(self.viewController.navigationController,"Navigation is nil")
}
2
  • it should be self.navigationController not self.viewController.navigationController Nov 12, 2015 at 7:53
  • self is FirstViewControllerTest script for unit test. If using self.navigationController is will become Value of type 'FirstViewControllerTest' had no member for 'navigationController'
    – user831098
    Nov 12, 2015 at 9:55

1 Answer 1

1

To test navigationController, you need to add it to array of view controllers.

func testViewControllerShouldHaveNavigationController() {
    let viewController = UIViewController()
    let navigation = UINavigationController()
    navigation.viewControllers = [viewController]
    XCTAssertNotNil(viewController.navigationController)
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.