我想花幾個小時左右的按鈕正常工作,並模仿後退按鈕。UIBarButtonItem初始化與自定義視圖選擇器不能正常工作
我的代碼來創建按鈕:
UIBarButtonItem *backButton = [self customBarButton:@"back_button" imageHiglighted:@"settings_button_highlighted" x:20 y:0 widthDivider:2.6 heightDivider:2.6];
backButton.target = self;
backButton.action = @selector(buttonPressed:);
self.navigationItem.leftBarButtonItem = backButton;
這裏所謂的創建自定義按鈕的方法:
- (UIBarButtonItem *)customBarButton:(NSString *)imageDefault imageHiglighted:(NSString *)imageHighlighted x:(float)x y:(float)y widthDivider:(float)widthDivider heightDivider:(float)heightDivider {
UIImage *customImageDefault = [UIImage imageNamed:imageDefault];
UIImage *customImageHighlighted = [UIImage imageNamed:imageHighlighted];
CGRect frameCustomButton = CGRectMake(x, y, customImageDefault.size.width/widthDivider, customImageDefault.size.height/heightDivider);
UIButton *customButton = [[UIButton alloc] initWithFrame:frameCustomButton];
[customButton setBackgroundImage:customImageDefault forState:UIControlStateNormal];
[customButton setBackgroundImage:customImageHighlighted forState:UIControlStateHighlighted];
UIBarButtonItem *barCustomButton =[[UIBarButtonItem alloc] initWithCustomView:customButton];
return barCustomButton;
}
和動作:
-(void)buttonPressed:(id) sender{
NSLog(@"Entered");
SearchViewController *ViewController = [[SearchViewController alloc] init];
[self.navigationController pushViewController:ViewController animated:YES];
}
所以我能使用簡單的UIButton而不是使用UIButtonBarItem,我真的不知道它是怎麼回事。
如果你能幫助我,我會非常感激。
謝謝。
的解決方案是增加: [的CustomButton addTarget:自我行動:@selector(buttonPressed :) forControlEvents:UIControlEventTouchUpInside] ; 在我打電話的方法裏面。 –