我在iPhone上的视图层次结构和绘制方面遇到了一些问题。
更具体地说,我有一个选项卡应用程序,其中包含一个表视图,我希望选择特定单元格时会出现UIPickerView。滑动不是问题(或者至少我假设一旦我解决了这个问题就不是问题了),但是我似乎无法让picker(或任何UIView)显示在选项卡栏上方。
我认为我设置这个特定选项卡的方式可能是问题所在。通常,在任何其他选项卡中,我可以像这样做:
[self.tabBarController.view addSubview:pickerView];
一切都可以正常工作。
但是对于这个特定的选项卡,我在导航栏中有一个UISegmentedControl,可以在两个不同的UITableView之间切换。因此,与该选项卡关联的视图控制器(称为TabViewController)具有其自己的这两个表视图控制器的实例(TableOneViewController和TableTwoViewController),并将当前选择的表视图(基于分段控件)插入为TabViewController视图的子视图。
如果我不需要像这样切换视图,我可以直接调用
[tabViewController.tabBarController.view addSubview:pickerView];
我想从TabViewController中调用选择器,并且选择器会显示在选项卡栏上方。但问题是,我不能在此选项卡中选择的任何一个表视图控制器中调用它(好吧,我可以,但它什么也不做)。我尝试将这个tabBarController属性传递到表视图控制器中,但那也不起作用。我还尝试过在应用程序委托中进行更改(我正在尝试避免这样做),但没有成功。
我是否遗漏了一些简单的东西,或者这不能完成?我感觉应该可以,因为在此表视图中键盘可以滑动到选项卡栏上方。是否有一种方法可以覆盖所有当前的视图和子视图?
以下是当在TabViewController.m中选择分段控件并切换视图时调用的内容:
- (IBAction)switchViews:(id)sender
{
if (self.tableOneViewController.view.superview == nil)
{
if (self.tableOneViewController == nil)
{
TableOneViewController *tempOneController = [[TableOneViewController alloc] initWithNibName:@"TableOneViewController" bundle:nil];
self.tableOneViewController = tempOneController;
[tempOneController release];
}
[tableTwoViewController.view removeFromSuperview];
[self.view insertSubview:tableOneViewController.view atIndex:0];
}
else
{
if (self.tableTwoViewController == nil)
{
TableTwoViewController * tempOneController = [[TableTwoViewController alloc] initWithNibName:@"TableTwoViewController" bundle:nil];
self.tableTwoViewController = tempOneController;
[tempOneController release];
}
[tableOneViewController.view removeFromSuperview];
[self.view insertSubview:tableTwoViewController.view atIndex:0];
}
}
当我尝试在TableOneViewController.m中添加选择器时,发生了以下情况:
UIPickerView *tempPicker = [[UIPickerView alloc] init];
tempPicker.delegate = self;
tempPicker.dataSource = self;
tempPicker.showsSelectionIndicator = YES;
tempPicker.clipsToBounds = NO; // thought this might work, but doesn't
self.picker = tempPicker;
[tempPicker release];
[self.view addSubview:pickerPicker]; // adds beneath tab bar!