Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
567 views
in Technique[技术] by (71.8m points)

iphone - Add image to UIBarButtonItem button

I have added UITabButtonItem onto my view controller using following code. Could anyone please tell me if its possible to associate image with it?

UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(favouriteButtonClicked:)]; 
viewController2.navigationItem.rightBarButtonItem = button; 
[button release];
[self.navigationController pushViewController:viewController2 animated:YES];

I use following code at other places but as in above code I am pushing viewcontroller onto my current view, this code doesn't work. I had to use above code.

UIButton *button =  [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:@"unselected.png"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(favouriteButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(280, 25, 30, 30)];
viewController2.navigationController.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];

[self.navigationController pushViewController:viewController2 animated:YES];

I don't know what is wrong but when I replace this custom button code with initWithBarButtonSystemItem, it works fine!!!

Finally I sorted out this issue and posted working code here but still I don't understand if I have a button in baseviewcontroller and I add it like following, it doesn't work!!! I had to use the code given in my accepted answer in viewwillappear!!

    [favButton addTarget:self action:@selector(favouriteButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
     self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];  

Following code still doesn't work!!! Don't know what's going on.

MPMoviePlayerViewController *videoController = [[[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:filePath]] autorelease];

UIButton* favouritebutton =  [UIButton buttonWithType:UIButtonTypeCustom];
[favouritebutton setBackgroundImage:[UIImage imageNamed:@"unselected.png"] forState:UIControlStateNormal];
[favouritebutton addTarget:self action:@selector(favouriteButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[favouritebutton setFrame:CGRectMake(280, 25, 30, 30)];
videoController.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:favouritebutton] autorelease]; 

[self presentMoviePlayerViewControllerAnimated:videoController];

Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
UIButton *favButton = [[UIButton alloc] init];

[favButton setImage:[UIImage imageNamed:@"unselected.png"] forState:UIControlStateNormal];
[favButton addTarget:self action:@selector(favouriteButtonClicked:)
   forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *button = [[UIBarButtonItem alloc]
                                initWithCustomView:favButton];

self.navigationItem.rightBarButtonItem = button;

[button release];
[favButton release];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...