How to get the embedded button of UIBarButtonItem
You can't. UIBarButtonItem
inherits from UIBarItem
which inherits from NSObject
. None of those classes has a method to get a UIButton
, because UIBarButtonItem
isn't a UIButton
.
Contrariwise, you can create a UIBarButtonItem
from a UIButton
-- see an answer I gave to a different question for more info (which works by adding it as a "custom view").
Accessing the "customView" property of the bar button item is one possible solution:
UIBarButtonItem *item = (UIBarButtonItem *)[self.navigationItem.rightBarButtonItems objectAtIndex:0];
OR
UIBarButtonItem *item = (UIBarButtonItem *)[self.navigationItem.rightBarButtonItem];
UIButton *myBtn;
if([item.customView isKindOfClass:[UIButton class]])
{
myBtn = (UIButton*)item.customView;
}
if(myBtn)
{
// do something
}
Try this out. It really works for me :)
I don't think you can get the embedded button in any case. However, if you create a button using a custom view, for example, using a button, we can get that button later using this code:
((UIButton *)(theBarButtonItem.customView.subviews.lastObject));
The hierarchy of the custom view should like this:
|--UIView
|--UIButton
|--UIImageView
Hopes that helps.