0

I am trying to create a custom navigation bar in my navigation controller that persists through each view after the initial navigation controller. I set up the navigation bar in a custom UINavigationController class, and I am trying to inherit the navigation controller's properties in my UIViewController custom class (the first View Controller after the nav controller)

Here is the syntax I am using, where HomePageViewController is my UIViewController that I want to inherit the Navigation Bar and numberInput is a UITextField in my HomePageViewController:

SearchNavigationController.swift

class SearchNavigationController: UINavigationController {

override func viewDidLoad() {
     super.viewDidLoad() 
     // Set up custom navigation bar
}

HomePageViewController.swift

class HomePageViewController: SearchNavigationController, UITextFieldDelegate, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var numberInput: UITextField!
override func viewDidLoad() {
    super.viewDidLoad()

    numberInput.delegate = self 
}

However, when my app calls numberInput.delegate = self, I get the error: fatal error: unexpectedly found nil while unwrapping an Optional value.

But when I replace SearchNavigationController with UIViewController it works.

3
  • UINavigationController is a "collection" of UIViewControllers. If your UINavigationController is already customised, all the UIViewControllers that are pushed in or pop off of your custom NavCon, will have the same navigation bar. Commented Jul 29, 2015 at 1:01
  • what are trying to do Commented Jul 29, 2015 at 1:28
  • @Lamar, as I stated in the first line I am trying to create a custom navigation bar in my navigation controller that persists through each view after the initial navigation controller. So replace the standard navigation controller that follows the header with my custom one. Commented Jul 29, 2015 at 17:11

1 Answer 1

2

A UINavigationController does not manage views -- it manages other UIViewControllers -- so it will not initialize any random @IBOutlets. It only cares about initializing its navigation bar and connecting to its child view controllers.

I think you misunderstand how to "inherit" (not the right word) your navigation bar in your child view controllers. The child view controllers must be a subclass of UIViewController. Then @IBOutlets act normally. As long as they are managed by a UINavigationController by virtue of creating the proper 'show' segues, they get decorated automatically with that UINavigationController's nav bar.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I tried this method originally but I could not figure out why the navigation controller was not being applied to the views after it. I realized the issue was that I had been using self.navigationController?.navigationBar rather than self.navigationBar.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.