Changing the UIBackButtonItem title

前端 未结 14 910
终归单人心
终归单人心 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:29

    Possibly already answered, but not simply.

    self.navigationItem.backBarButtonItem defaults to nil. So if you set the title to it, nothing will happen because it is affecting a nil address object. This won't throw an error, because objective-c allows messaging nil objects.

    ANSWER: You must create your own button object in order to set it's title. You can initWithTitle or create it and then set the title afterward.

    PS - as mentioned, the backBarButtonItem only affects it's child views that are pushed on top of it in the navigation stack. Depending on how you have your app architected, the root view can't be popped any further and can't have a back button. Though, you can affect the leftBarButtonItem.

提交回复
热议问题