Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift - Title Not Appearing for Navigation View Controller

I have a navigation view controller that upon action sends a segue to a tab bar view controller. Because of this segue the tabbed view controller inherits the navigation bar. I am trying to apply a title to one of my view controllers attached to the tab bar view controller, but setting the title via code is not working for me. does anyone know the reason why that could be?

Here is a picture of my storyboard:

storuboard

The view controller with the logout button is where I am trying to set the title in the nav bar (code):

import UIKit

class ProfileSettingsViewController: UIViewController {

    override func viewWillAppear(animated: Bool) {

        self.navigationItem.title = "Profile Settings"

    }

    override func viewDidLoad() {
        super.viewDidLoad()

        self.navigationItem.leftBarButtonItem = nil


    }



    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }



    @IBAction func logoutButton(sender: AnyObject) {

        PFUser.logOut()
        var currentUser = PFUser.currentUser()
        self.performSegueWithIdentifier("userLoggedOut", sender: self)
        self.navigationController?.setNavigationBarHidden(self.navigationController?.navigationBarHidden == false, animated: true)

    }

}

View controller embedded in the navigation controller triggering the segue to the tab bar controller:

import UIKit

class UserRegistrationViewController: UIViewController {


    func displayAlert(title:String, error:String) {

        var alert = UIAlertController(title: title, message: error, preferredStyle: UIAlertControllerStyle.Alert)

        alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: {
            action in



        }))

        self.presentViewController(alert, animated: true, completion: nil)


    }

    @IBOutlet var usernameTextField: UITextField!

    @IBOutlet var emailTextField: UITextField!

    @IBOutlet var passwordTextField: UITextField!


    override func viewDidLoad() {
        super.viewDidLoad()


    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    @IBAction func registerUser(sender: AnyObject) {

        var error = ""

        if usernameTextField.text == nil || emailTextField.text == nil || passwordTextField.text == nil {

            error = "Please enter a username, email and password"

        }


        if error != "" {

            displayAlert("Error In Form", error: error)

        } else {

            var user = PFUser.currentUser()

            user.username = usernameTextField.text
            user.password = passwordTextField.text
            user.email = emailTextField.text

            user.saveInBackgroundWithBlock {
                (succeeded: Bool!, signupError: NSError!) -> Void in
                if signupError == nil {

                    println(user.username)
                    println(user.password)
                    println(user.email)


                    self.performSegueWithIdentifier("successfulRegistration", sender: self)

                    self.navigationController?.setNavigationBarHidden(self.navigationController?.navigationBarHidden == false, animated: true)

                    // Hooray! Let them use the app now.
                } else {

                    if let errorString = signupError.userInfo?["error"] as? NSString {
                        error = errorString
                    } else {

                        error = "Please try again later."

                    }


                    self.displayAlert("Could Not Sign Up", error: error)

                }
            }


        }


    }



}
like image 875
cphill Avatar asked Apr 16 '15 13:04

cphill


2 Answers

This worked for me: self.parent?.title = "nav bar title"

like image 74
Rehaan Avatar answered Nov 16 '22 03:11

Rehaan


In your view controller hierarchy, navigation bar is displaying the title of UITabBarController, not view controllers inside the UITabBarController.

Easiest way to get title shown in navigation bar would be

override func viewWillAppear(animated: Bool) {

    self.tabBarController?.navigationItem.title = "Profile Settings"

}
like image 33
Jakub Vano Avatar answered Nov 16 '22 02:11

Jakub Vano