1

Added UIButton to app

UIButton *playButton = [UIButton buttonWithType:UIButtonTypeCustom];

[playButton addTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];

UIImage *image = [UIImage imageNamed:@"play.png"];

[playButton setImage:image forState:UIControlStateNormal];

when i run the application it is crashing and message is UIButtonView unrecognized selector

Did exception breakpoint

Found that it is crashing at

[toolbar setItems:toolbarItems];

I think i m not adding UIButton properly to toolbar

//Add buttons to the array

NSArray *toolbarItems = [NSArray arrayWithObjects: settingsButton, flexItem, rewind, flexItem, playButton, flexItem, pause, flexItem, modalBarButtonItem, nil];

 [toolbar setItems:toolbarItems];

Anyideas to fix this one.

Thanks for help.

Rishil Patel
  • 1,977
  • 3
  • 14
  • 30
user1120133
  • 3,244
  • 3
  • 48
  • 90

2 Answers2

1

The problem is that you can't directly stick a UIButton into a UIToolBar. You have to encapsulate the UIButton in a UIBarButtonItem's view and then stick your custom UIBarButtonItem in the UIToolBar. If you don't need a custom look or functionality, you may want to look into using a standard UIBarButtonItem because it looks nice without much work on your part.

See Adding a UILabel to a UIToolbar for an example.

Community
  • 1
  • 1
Jack Lawrence
  • 10,664
  • 1
  • 47
  • 61
  • UIBarButtonItem *play = [[UIBarButtonItem alloc] initWithCustomView:playButton]; self.navigationItem.leftBarButtonItem = play; i made these changes now app is not crashing but dont see uibutton – user1120133 Feb 01 '12 at 18:26
0

UIToolbar contains instances of UIBarButtonItem, not UIButton. If you just need a custom image, try:

UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(play:)];

// Build your array and setItems: using "item" among other objects

[item release];
Rishil Patel
  • 1,977
  • 3
  • 14
  • 30
Conrad Shultz
  • 8,748
  • 2
  • 31
  • 33