0

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?

zakdances
  • 22,285
  • 32
  • 102
  • 173

4 Answers4

3

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.

Tricertops
  • 8,492
  • 1
  • 39
  • 41
  • For anyone trying this with a UINavigationBar, it really doesn't like UIBarButtonItem (unrecognized selector). So try a UINavigationItem instead. – zakdances Apr 03 '13 at 20:41
0

I got the same error, try making the object a button instead of just an item. This code worked fine for me

UIBarButtonItem *title = [[UIBarButtonItem alloc] init];
[title setTitle:@"My Title"];

If you're trying to give the toolbar a title, make a UILabel with the title and add it as a subview.

Chris Loonam
  • 5,735
  • 6
  • 41
  • 63
  • Those two calls are completely equivalent. –  Apr 03 '13 at 18:59
  • The difference between mine and his is that his is a `UIBarItem`, mine is a `UIBarButtonItem`. I tried it, his code didn't work, and when I tested mine it worked. – Chris Loonam Apr 03 '13 at 19:00
  • But OP wants a plain `UIBarItem`, doesn't he? –  Apr 03 '13 at 19:02
  • Yes, but it looks like you cant set the title for a `UIBarItem`, for some reason it gives an error. It might be a bug, but it doesn't work for some reason. I tried with dot-syntax and `[title setTitle:]`, but neither worked. – Chris Loonam Apr 03 '13 at 19:04
  • This answer does not explain the problem. – Tricertops Apr 03 '13 at 19:15
0

From Apple's documentation:

title

The title displayed on the item.

@property(nonatomic, copy) NSString *title

Discussion: You should set this property before adding the item to a bar. The default value is nil.

Availability: Available in iOS 2.0 and later.

So this title can indeed be set. Are you sure it is the UIBarItem class that is displayed as the part of the error message? Can be either a memory management problem, or if not, a bug in UIKit (very unlikely).

-1

I believe you should be using either initWithTabBarSystemItem:tag: or initWithTitle:image:tag: to initialize your UIBarItem.

Jon Brooks
  • 2,472
  • 24
  • 32
  • No. [The official documentation says the opposite.](http://developer.apple.com/library/ios/documentation/uikit/reference/UIBarItem_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40007520-CH3-SW20) –  Apr 03 '13 at 18:56
  • Sorry, I was looking at UITabBarItem. UIBarItem is documented as an abstract superclass. – Jon Brooks Apr 03 '13 at 18:59
  • No problem, it's an understandable mistake :) –  Apr 03 '13 at 19:00
  • So, to reiterate - I think the problem is that your trying to instantiate an abstract class, which you shouldn't do, though I don't know offhand if it would cause the problem you're seeing. – Jon Brooks Apr 03 '13 at 19:07