Changing the UIBackButtonItem title

前端 未结 14 888
终归单人心
终归单人心 2020-12-04 17:48

I have a navigationController-based app. I want to change the title of the back button for the root view controller. I have tried the following code in the rootViewControlle

14条回答
  •  独厮守ぢ
    2020-12-04 18:40

    As a previous poster (Brenden) mentioned, by default the value of backBarButtonItem is nil, so setting its title results in sending a message to a nil object which of course gets ignored.

    If you don't want to muck around with manually creating a new button in order to set its title, there is a work around in Interface Builder.

    In IB, for a given view controller that's part of a navigationController stack, you can click on the navigationItem representing the navigationBar. When you click, the properties inspector lets you set three properties: Title, Prompt and BackButton.

    If, in the backButton field you type in any old random text, this has the effect of instantiating a backBarButtonItem object and in your code, you are now able to set its title to any text you want.

    Hope this helps someone.

提交回复
热议问题