1

在 viewDidLoad: - (void)viewDidLoad { [super viewDidLoad];

[self.navigationController setNavigationBarHidden:YES animated:NO];

UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
NSMutableArray *toolBarItems = [[NSMutableArray alloc] init];
[toolBarItems addObject:[[UIBarButtonItem alloc] initWithTitle:@"Articles" style:UIBarButtonItemStyleBordered target:self action:@selector(backButtonTapped)]];
[toolBarItems addObject:[[UIBarButtonItem alloc] initWithTitle:@"Source" style:UIBarButtonItemStyleBordered target:self action:nil]];
[toolBarItems addObject:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil]];
[toolBarItems addObject:[[UIBarButtonItem alloc] initWithTitle:@"Aa" style:UIBarButtonItemStyleBordered target:self action:nil]];
[toolBarItems addObject:[[UIBarButtonItem alloc] initWithTitle:@"Rabbit" style:UIBarButtonItemStyleBordered target:self action:nil]];
[toolBar setBackgroundImage:[[UIImage alloc] init] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
toolBar.items = toolBarItems;
[self.view addSubview:toolBar];

方法:

- (void)backButtonTapped {
    [self.navigationController popViewControllerAnimated:YES];
}

我在方法内部的调用上有一个断点,但它永远不会被调用。为什么这个方法永远不会被调用?

4

3 回答 3

5

我在整个视图上有一个 UITapGestureRecognizer,它拦截了 UIBarButton 上的点击。由于这个答案,我解决了这个问题,它基本上从一开始就停止了 UITapGestureRecognizer,除非它在 ​​UIToolBar 之外。

于 2013-04-28T15:19:21.187 回答
1

Your code is not complete and I dont know if you didn't implement it or just didn't wrote it here, so just to make sure, this should work:

UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
NSMutableArray *toolBarItems = [[NSMutableArray alloc] init];
[toolBarItems addObject:[[UIBarButtonItem alloc] initWithTitle:@"Articles" style:UIBarButtonItemStyleBordered target:self action:@selector(backButtonTapped)]];
toolBar.items = toolBarItems;
[self.view addSubview:toolBar];
于 2013-04-27T23:15:51.937 回答
0

像这样试试。它工作正常。

 UIToolbar *toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
    UIBarButtonItem *infoButton = [[UIBarButtonItem alloc]initWithTitle:@"back"style:UIBarButtonItemStyleBordered target:self action:@selector(backButtonTapped)];
    [toolbar setItems:[NSArray arrayWithObjects:infoButton,nil]];
    [self.view addSubview:toolbar];
于 2013-04-28T06:19:36.110 回答