1

In order to get the trash image displayed in a standard toolbar on iOS. I am using the following code:

    UIBarButtonItem *tempTBButn=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:nil action:nil];
    UIImage *trashImg=tempTBButn.image;

But it does not work. The result I get in trashImg is just nil. What should I do to obtain the result I want? That is to have the trash icon in trashImg.

Michel
  • 10,303
  • 17
  • 82
  • 179

1 Answers1

1

The image property is only set when you create a UIBarButtonItem with one of the custom image init methods like:

UIBarButtonItem *customImageBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"yourImage"] style:UIBarButtonItemStylePlain target:self action:@selector(barButtonItemPressed:)];

The image property defaults to nil when you use the initWithBarButtonSystemItem: method. Your best bet is to just use a trash icon of your own, or work with just having the standard icon on the nav.

Tommy Devoy
  • 13,441
  • 3
  • 48
  • 75
  • OK, it is clear. The fact is I want this little icon to use somewhere else (on a UIButton of my own). I thought there should a simple way to get the image. – Michel Sep 04 '14 at 05:14
  • Actually I do not (at least up to now) mark the answer as accepted for the sole reason it helps me. I mark it as accepted if it solves my problem. If it only gives me a little hint, or if (like this is the case here) it tells me that there is no solution for what I want to do. Even though it is surely helpful I do not mark it as accepted. I would rather mark it as useful (with +1). It is a way for me to make the difference between "The answer" and "A useful help". But I can easily adjust this if this is the standard on this site. – Michel Oct 07 '14 at 06:12