UIToolbarItem在作为UIPickerView的子视图插入时未选择

时间:2014-03-28 14:11:02

标签: objective-c uipickerview uitoolbar subview addsubview

我有一个UIPickerView,它将UIToolbar作为子视图放置。问题是当我点击UIbarviewItem时没有任何反应。我不知道为什么,但如果我将UIToolbar作为子视图插入主视图([self.view addSubview:toolbar]),按钮工作正常,但不能作为uIPickerView的子视图?

这是我用来创建uipickerview和uitoolabr的代码:

dateRangePickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.view.frame)-200, CGRectGetWidth(self.view.frame), 220)];
dateRangePickerView.showsSelectionIndicator = YES;
dateRangePickerView.hidden = NO;
[dateRangePickerView reloadAllComponents];

dateRangePickerView.showsSelectionIndicator = YES;
dateRangePickerView.delegate = self;
dateRangePickerView.dataSource = self;
[dateRangePickerView setBackgroundColor:[UIColor whiteColor]];

[self.view addSubview:dateRangePickerView];

// Add toolbar
toolBar= [[UIToolbar alloc] initWithFrame:CGRectMake(0, -44, CGRectGetWidth(self.view.frame), 44)];
[toolBar setBarStyle:UIBarStyleDefault];
UIBarButtonItem *barButtonDone = [[UIBarButtonItem alloc] initWithTitle:@"Done"
                                                                  style:UIBarButtonItemStyleBordered target:self action:@selector(selectDateRange)];
toolBar.items = [[NSArray alloc] initWithObjects:barButtonDone,nil];
barButtonDone.tintColor=[UIColor blackColor];
[dateRangePickerView addSubview:toolBar];

有人能告诉我哪里出错了?

谢谢,

彼得

1 个答案:

答案 0 :(得分:0)

我决定不向UIPicker添加子视图,而是将UIPicker和UIToolbar放在UIView中以保持一致。

感谢大家的帮助:)