将图像添加到UIBarButtonItem按钮 [英] Add image to UIBarButtonItem button
问题描述
我已使用以下代码将UITabButtonItem添加到我的视图控制器上。任何人都可以告诉我是否可以将图像与它相关联?
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];
我在其他地方使用以下代码但是在上面的代码中我将viewcontroller推到我当前的视图中,这段代码不起作用。我不得不使用上面的代码。
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];
我不知道有什么问题但是当我用initWithBarButtonSystemItem替换这个自定义按钮代码时,它可以工作很好!!!
I don't know what is wrong but when I replace this custom button code with initWithBarButtonSystemItem, it works fine!!!
最后我整理了这个问题,并在这里发布了工作代码,但我仍然不明白我是否在baseviewcontroller中有一个按钮,我将其添加如下,它不起作用!我不得不在viewwillappear中使用我接受的答案中给出的代码!!
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];
谢谢。
推荐答案
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];
这篇关于将图像添加到UIBarButtonItem按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!