我有一个在界面构建器中创建的UIPickerview。并通过代码我将工具栏作为其子视图和一个完成按钮作为工具栏栏按钮项。我想在单击完成按钮时执行操作(隐藏选择器视图和一些额外的东西)。但由于某种原因,完成按钮的操作无效。以下是在viewDidLoad中创建工具栏和条形按钮项的代码。
UIToolbar *toolBar= [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,320,44)];
toolBar.barTintColor = [UIColor colorWithRed:70/250.0f green:70/250.0f blue:70/250.0f alpha:1.0];
UIBarButtonItem *barButtonDone = [[UIBarButtonItem alloc] initWithTitle:@"Done"
style:UIBarButtonItemStyleDone target:self action:@selector(doneButtonTapped:)];
UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
toolBar.items = @[flexible, barButtonDone, flexible];
barButtonDone.tintColor=[UIColor colorWithRed:227/255.0f green:178/255.0f blue:49/255.0f alpha:1.0];
[picker addSubview:toolBar];
[picker bringSubviewToFront:toolBar];
这是方法doneButtonTapped:
-(void)doneButtonTapped:(UIBarButtonItem *)sender{
NSLog(@"Done button tapped");
picker.hidden = true;
[self.view bringSubviewToFront:txtCity];
[self.view bringSubviewToFront:advancedSearchLine];
[self.view bringSubviewToFront:txtCityLine];
}
我在运行应用程序时检查了布局调试器,实际上完成按钮位于视图的前面,因此前面没有任何内容可以防止它工作。我做错了什么?
答案 0 :(得分:0)
您无法将工具栏添加为选取器视图的子视图。在UIView中添加工具栏并将选取器创建为子视图。
UIView *inputView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, toolBar.frame.size.height + picker.frame.size.height)];
inputView.backgroundColor = [UIColor clearColor];
[inputView addSubview:picker];
[inputView addSubview:toolBar];