Search code examples
iosxcodeuibarbuttonitemuinavigationitem

ios/xcode: change title of bar button item


I want to change the title of a bar button (right navigation) from login to sign up depending on which the user checks.

Previously, I had this as a button in the VC and it worked with the following code:

[_submitButton setTitle:@"Sign Up" forState:UIControlStateNormal];

However, now that I've deleted the button in the page and have given the bar button the name submitButton, I am getting the following error:

No visible @interface for 'UIBarButtonItem' declares the selector 'setTitleForState'

Does anyone know why this is not working? Do I have to name uibarbutton something different than a regular button?


Solution

  • UIBarButtonItem is not UIButton

    Please check the following:

    • option-click on _submitButton, you will see a popover with declaration and type.
    • click on UIBarButtonItem, you will see the documentation.
    • there is no method at all to set the title.
    • look at the line Inherits from: on the top under the big class name.
    • UIBarButtonItem is a subclass of UIBarItem
    • click on UIBarItem
    • There is a title property without further arguments, so it's

      _submitButton.title = @"Sign Up";
      

    Reading the documentation is very important, it's impossible to remember all properties/methods of all classes but it's the first thing you should do if you get an error message like this.