What's the different with titles?

761 Views Asked by At

I would like to add title to my UIViewController:

I have tried

self.navigationController.navigationItem.title
self.navigationController.title
self.title

Sometimes, solution 1 work, sometimes solution 2 work, sometimes solution 3 works.

Can any expert tell me the different between them?

2

There are 2 best solutions below

0
On BEST ANSWER

title is a property of UIViewController.

A localized string that represents the view this controller manages. Set the title to a human-readable string that describes the view. If the view controller has a valid navigation item or tab-bar item, assigning a value to this property updates the title text in those objects.


self.navigationController is a UINavigationController that manages the stack of viewControllers that your viewController is in. UINavigationController is a subclass of UIViewController, so self.navigationController.title is the title of the UINavigationController.


self.navigationItem.title:

The navigation item’s title displayed in the center of the navigation bar. The default value is nil. When the receiver is on the navigation item stack and is second from the top—in other words, its view controller manages the views that the user would navigate back to—the value in this property is used for the back button on the top-most navigation bar. If the value of this property is nil, the system uses the string “Back” as the text of the back button.


So, in practice, you should set the title of your ViewControllers. iOS will copy this title to the navigation item or tab bar item and display this title on the Navigation Bar if your ViewController is managed by a UINavigationController, it will use that text for the Back Button when you push to another ViewController, and it will display it in the tab bar if your ViewController is managed by a UITabBarController.

2
On

I create a demo to explain them:

enter image description here

You see, my vc1 is yellow-gray color embed in a navigation controller,my vc2 is light-green color embed in a navigation controller too, the two navigation controller is all manage by a tabbar controller .

In ViewController.swift(it is vc1), if I set self.title:

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    self.title = "vc1's title"  
    }

}

In ViewController2.swift(it is vc2):

import UIKit

class ViewController2: UIViewController {

       override func viewDidLoad() {
        super.viewDidLoad()

        self.title = "vc2's title"
    }

}

The result is the tabbar title and navigation title all set:

enter image description here

If I set self.navigationController?.title:

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // self.title = "vc1's title"

        self.navigationController?.title = "vc1's nav title"

    }
}

The result is tabbar title is set:

the result

If I set self.navigationItem.title:

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // self.title = "vc1's title"

        //self.navigationController?.title = "vc1's nav title"

        self.navigationItem.title = "vc1's navItem title"
    }
}

The result is navigation title is set:

enter image description here