iPhone视图层次结构问题:如何在标签栏上绘制视图?

时间:2021-02-24 20:23:55

I am having some trouble with view hierarchies and drawing on the iPhone.

我在视图层次结构和在iPhone上绘图时遇到了一些麻烦。

To be more specific, I have a tab bar application with a certain tab that contains a table view where I would like the selection of a specific cell to have a UIPickerView slide up. The sliding isn't really a problem (or at least I'm assuming it won't be once I figure this part out), but I cannot seem to get the picker (or any UIView, for that matter) to show up over the tab bar.

更具体地说,我有一个带有特定选项卡的选项卡条应用程序,该选项卡包含一个表视图,我希望选择一个特定的单元格,使UIPickerView向上滑动。滑动并不是真正的问题(或者至少我假设一旦我算出这部分就不是),但是我似乎不能让选择器(或者任何UIView)出现在选项卡栏上。

I think the way I have this certain tab set up may be the problem. Normally, in any other tab, I could just do something like this:

我认为我设置这个标签的方式可能是问题所在。通常,在其他选项卡中,我可以这样做:

[self.tabBarController.view addSubview:pickerView];

and everything would work fine.

一切都会好起来的。

But for this specific tab, I have a UISegmentedControl in the navigation bar that switches between two different UITableViews. So the view controller associated with the tab (call it TabViewController) has its own instances of these two table view controllers (TableOneViewController and TableTwoViewController) and will insert the currently selected table view (based on the segmented control) as a subview of TabViewController's view.

但是对于这个特定的选项卡,我在导航条中有一个UISegmentedControl在两个不同的uitableview之间切换。因此,与选项卡相关联的视图控制器(称为TabViewController)拥有这两个表视图控制器(TableOneViewController和TableTwoViewController)的实例,并将当前选定的表视图(基于分段控件)插入到TabViewController的视图的子视图中。

If I didn't need to switch the views like this I could just call

如果我不需要像这样切换视图,我可以调用

[tabViewController.tabBarController.view addSubview:pickerView];

from TabViewController and the picker would show up over the tab bar. But the thing is I cannot call this in either of the table view controllers selected within this tab (well I can, but it doesn't do anything). I have tried passing this tabBarController property into the table view controller, but that doesn't work either. I have also tried messing around with the app delegate (which I'm trying to avoid) to no avail.

从TabViewController和picker会出现在标签栏上。但是,我不能在这个选项卡中选择的表视图控制器中调用这个(我可以,但是它什么都不做)。我已经尝试将这个tabBarController属性传递给表视图控制器,但这也不起作用。我也尝试过使用app委托(我尽量避免),但没有任何效果。

Is there something simple I'm missing here, or can this not be done? I feel like it should since a keyboard can slide up over the tab bar in this table view. Is there a way to just draw over all the current views and subviews?

有什么简单的东西我错过了,还是这不能做?我觉得应该这样,因为键盘可以在这个表格视图的标签栏上滑动。有没有一种方法可以画出所有当前视图和子视图?

Here is what is called when the segmented control is selected within TabViewController.m, and switches the views:

这是在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];
    }
}

And here's what's going on when I try to add the picker in TableOneViewController.m:

这是我在tableoneviewcontroller中添加picker时发生的情况。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!

2 个解决方案

#1


7  

[[[UIApplication sharedApplication] keyWindow] addSubview:someViewController];

Make sure the viewController you are loading in is 480px high!

确保你正在加载的viewController是480px高!

To remove it again, use [someViewController removeFromSuperview];

要再次删除它,请使用[someViewController removeFromSuperview];

#2


2  

Maybe you want to do just

也许你想这样做

[tabBarController presentModalViewController:someViewController animated:YES];

?

吗?

This should slide on top of anything else you have on the screen.

这应该可以在屏幕上其他任何东西的顶部滑动。

#1


7  

[[[UIApplication sharedApplication] keyWindow] addSubview:someViewController];

Make sure the viewController you are loading in is 480px high!

确保你正在加载的viewController是480px高!

To remove it again, use [someViewController removeFromSuperview];

要再次删除它,请使用[someViewController removeFromSuperview];

#2


2  

Maybe you want to do just

也许你想这样做

[tabBarController presentModalViewController:someViewController animated:YES];

?

吗?

This should slide on top of anything else you have on the screen.

这应该可以在屏幕上其他任何东西的顶部滑动。