Search code examples
iosobjective-cruntime-erroruitoolbarunrecognized-selector

[UIBarItem setTitle:]: unrecognized selector sent to instance


I'm trying to give my UIToolBar a title, but when I try to add a UIBarItem with a title attribute, I get this error. My code:

UIBarItem *title = [[UIBarItem alloc] init];
title.title = @"My Title";

[toolBar setItems:@[cancel, title, ok]];

I've check the official docs for UIBarItem, and it does indeed have a perfectly valid "title" attribute. What am I doing wrong here?


Solution

  • I think the problem is, that UIBarItem is an abstract superclass of UIBarButtonItem and UITabBarItem.

    Maybe the method is just declared in header and not implemented or the abstract implementation calls -doesNotRecognizeSelector:.

    UIBarItem is an abstract superclass for items added to a bar that appears at the bottom of the screen. …

    For -[UIToolbar items] or UINavigationBar -[UINavigationItem (left|right)BarButtonItems], you have to use use UIBarButtonItem.