How to add shortcut and image to toolbar and set properties in code
I would like to show a label ( UILabel
and an image ( UIMageView
) on the toolbar ( UIToolbar
). In the Builder interface, I added a panel button item ( UIBarButtonItem
) for each of these views, and dragged a blank item ( UIView
) onto each of the panel items. See screenshot below for hierarchy representation.
I am assuming that each of these views has become customViews
which I should be able to access in code with
UIView *labelBarButtonView = (UIView*)labelBarButtonItem.customView;
and then view the label or image with
UILabel *companyLabel = (UILabel*)[labelBarButtonItem.customView viewWithTag:tag];
However, when flies with an error code starting my application: 'NSInvalidArgumentException', reason: '-[UIView customView]: unrecognized selector sent to instance
.
What did I miss? What would be the best way to create views in Interface Builder and access their properties in code?
Thank!
source to share
Okay, I think this is your problem. In the line of code:
UIView *labelBarButtonView = (UIView*)labelBarButtonItem.customView;
You are actually submitting labelBarButtonItem
to UIView
and then requesting customView
. It looks like you would choose customView
, but you are not. So try using this code:
UIView *labelBarButtonView = (UIView*)((UIBarButtonItem *)labelBarButtonItem.customView);
The above code casts labelBarButtonItem
, pulls customView
, then castes. Then, to get the shortcut, you can access it:
UILabel *companyLabel = (UILabel*)[labelBarButtonView viewWithTag:tag];
source to share