Here's how I set the associated object:
objc_setAssociatedObject(navigationItem, "rightButton", leftButtonView, OBJC_ASSOCIATION_RETAIN);
and here's how I get it:
UIButton *favoriteButton = (UIButton *)objc_getAssociatedObject(self.navigationItem, "rightButton");
The object is a UIButton
in both instances, and I have the #import "objc/runtime.h"
in both classes.
The thing is, it used to work, but now for some reason it returns null
. Any ideas?
EDIT
I'm setting the associated object in another class by passing the navigationItem
as an argument:
+ (void)setNavigationBarTitle:(NSString *)title
andLeftButton:(NSString *)leftButtonTitle
withSelector:(SEL)leftSelector
andRightButton:(NSString *)rightButtonTitle
withSelector:(SEL)rightSelector
andTarget:(id)target
forNavigationItem:(UINavigationItem *)navigationItem;
I use this convenience method in my utility class to set up a navigation bar for each new view controller in my app. When creating the navigation bar buttons, I associate them with the navigation item - my first line of code in the question.