我已经使用以下代码在我的视图控制器上添加了UITabButtonItem。请问是否可能将图片与其关联起来?
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(favouriteButtonClicked:)];
viewController2.navigationItem.rightBarButtonItem = button;
[button release];
[self.navigationController pushViewController:viewController2 animated:YES];
我在其他地方使用以下代码,但是因为在上面的代码中我将视图控制器推入到当前视图中,所以这段代码不起作用。我必须使用上面的代码。
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替换这个自定义按钮代码时,它可以正常工作!!!
最终我解决了这个问题并在此发布了可用代码,但我仍然不明白为什么如果我在baseviewcontroller中有一个按钮,并像下面这样添加它,它就不能工作!!!我必须在viewwillappear中使用我的已接受答案中给出的代码!!
[favButton addTarget:self action:@selector(favouriteButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];
以下代码仍然无法正常工作!!! 不知道发生了什么。
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];
谢谢。
viewDidLoad
ж–№жі•дёе€ќе§‹еЊ–жЊ‰й’®пјџ - Maggie