将 UIView 子视图添加到导航堆栈中的单个表视图控制器

发布于 2024-09-27 05:42:15 字数 848 浏览 7 评论 0 原文

我正在开发一个在导航堆栈中具有三个表视图控制器的应用程序。根视图控制器和第二个 VC 有工具栏,但我想向第二个视图控制器添加一个子视图 像这样 。 (颜色只是为了可视化。)

我想以编程方式添加视图,因为我无法使用 IB 来做到这一点,而不会遇到很大的麻烦。现在,我已经能够通过在第二个视图控制器中绘制 UIView 来获得我想要的东西,如下所示:

- (void)viewDidLoad {
      [super viewDidLoad]

      UIView *detailView = [[UIView alloc] initWithFrame:CGRectMake(0, 392, 320, 44)];
      detailView = [UIColor redColor];
      [self.navigationController.view addSubview:detailView];
      [detailView release];
}

这种方法的问题是,一旦 UIView 加载到第二个视图控制器中,它就会保持加载状态,并且在第三个和根视图控制器中绘制。我尝试了多种删除UIView的方法,包括在viewDidUnload中将detailView设置为nil,在didSelectRowAtIndexPath中调用removeFromSuperview(这从整个堆栈中删除了视图)。

我还尝试将子视图添加到 self.view 中,但这会将其推到表视图的可见区域下方,因此我必须向上滚动才能看到它,当我放手时它会重新向下滚动。

显然,将此子视图添加到导航控制器并不是执行我想要的操作的最佳方法,但我不知道从这里该去哪里。

I'm working on an app that has three table view controllers in a navigation stack. The root view controller and the second VC have toolbars, but I want to add a subview to the second view controller like this. (The color is just there for visualization.)

I want to add the view programmatically, since I haven't been able to do it with IB without major headaches. Right now, I've been able to kind of get what I want by drawing a UIView in the second view controller like this:

- (void)viewDidLoad {
      [super viewDidLoad]

      UIView *detailView = [[UIView alloc] initWithFrame:CGRectMake(0, 392, 320, 44)];
      detailView = [UIColor redColor];
      [self.navigationController.view addSubview:detailView];
      [detailView release];
}

The problem with this approach is that once the UIView is loaded in the second view controller, it stays loaded and is drawn in the third and root view controllers. I've tried a variety of methods of removing the UIView, including setting the detailView to nil in viewDidUnload, calling removeFromSuperview in didSelectRowAtIndexPath (which removed the view from the whole stack).

I've also tried adding the subview to self.view, but that pushes it below the visible area of the table view, so I have to scroll up to see it, and it snaps back down when I let go.

Clearly, adding this subview to the navigation controller is not the best way to do what I want, but I'm at a loss as to where to go from here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

流年里的时光 2024-10-04 05:42:15

正如您已经发现的,您绝对不应该进入导航控制器的视图。

您希望您的 SecondViewController 成为一个实现 UITableViewDelegate 和 UITableViewDataSource 的 UIViewController ,其视图布局 UITableView 和您希望用于固定“页脚”的 UIView自己的主 UIView。

记住 UITableViewController 最终只是为了方便创建视图完全由 UITableView 组成的视图控制器。

无论如何,您可以从 此只读 svn 存储库

编辑(现在不是午夜,直接在答案中放入一些代码/解释):

为了将控制器推送到需要页脚的导航堆栈上,创建一个新的基于 UIViewController 的类(不要检查模板选择对话框中的“UITableViewController 子类”框)。

UITableView 和要成为额外底部视图的 UIView 添加实例变量。

@interface SecondViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
    UITableView* tableView;
    UIView* customFooterView;
}
@property (nonatomic,retain) IBOutlet UITableView* tableView;
@property (nonatomic,retain) IBOutlet UIView* customFooterView;
@end

在 IB 中,将 UITableView 和 UIView 添加到控制器的现有根视图中,并根据需要对它们进行布局(如果您的应用程序可以横向和纵向使用,则可能也值得更改自动调整大小参数)。将两个视图连接到“文件所有者”中为它们定义的出口,并确保连接 UITableView 的委托和数据源属性以指向“文件所有者”。

然后只需根据您的应用程序实现 UITableViewDelegate 和 UITableViewDataSource 协议即可。

如果您想在 IB 中布置整个“页脚”视图,请继续。否则,您可以轻松地在 viewDidLoad 中以编程方式添加项目(并记住在 viewDidUnload 中将其拆除)。

As you've already discovered, you definitely should not be reaching up into the navigation controller's view.

You want your SecondViewController to be an UIViewController that implements the UITableViewDelegate and UITableViewDataSource and whose view lays out the UITableView and the UIView you wish to use for your stationary 'footer' in it's own main UIView.

It helps to keep in mind that UITableViewController is ultimately is just a convenience for creating a view controller whose view consists entirely of a UITableView.

Anyway, rather than attempt to put a pile of that code inline in this answer, you can browse it (or svn co) from this read-only svn repo.

EDITED (now that it's not midnight, putting some code/explanation directly in answer):

For the controller to be pushed onto the nav stack that needs the footer create a new UIViewController-based class (do NOT check the 'UITableViewController subclass' box in the template selection dialog).

Add instance variables for the UITableView and the UIView that is to be the extra bottom view.

@interface SecondViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
    UITableView* tableView;
    UIView* customFooterView;
}
@property (nonatomic,retain) IBOutlet UITableView* tableView;
@property (nonatomic,retain) IBOutlet UIView* customFooterView;
@end

In IB add a UITableView and UIView to the existing root view for the controller and lay them out as desired (probably worth altering the auto-resize parameters too if your app can be used in both landscape and portrait). Hook up the two views to the outlets defined for them in the "File's Owner" and also ensure you hook up the UITableView's delegate and dataSource properties to point at the "File's Owner."

Then just implement the UITableViewDelegate and UITableViewDataSource protocols as appropriate for your application.

If you want to lay out the entire 'footer' view in IB then go right ahead. Otherwise you can easily add items programmatically in viewDidLoad (and remember to tear it down in viewDidUnload).

失去的东西太少 2024-10-04 05:42:15

我不喜欢这种方法。您应该将表格视图放入另一个视图中,并将详细信息视图放在该视图中。

尽管如此,我认为您可以在视图控制器的 viewWillDisappear 方法中删除视图。我还注意到您没有将 detailView 保留为私有变量,您应该这样做,因为稍后删除它时需要引用它(我仍然想知道您是如何做到的。)

请注意 < code>viewDidUnload 在视图卸载(即从控制器中释放)时调用,因此它与导航无关。

I don't like the approach. You should put your table view inside another view, and put your detail view together in that view.

Despite of that, I think you can remove your view in viewWillDisappear method of your view controller. I also notice that you did not keep your detailView as a private variable, which you should do because you need to reference it when removing it later (I still wonder how you have done it.)

Note that viewDidUnload is called in case of view unloading (i.e. releasing from its controller), so it is not related to navigation.

各空 2024-10-04 05:42:15

不确定您要寻找哪种行为,但请尝试以下其中一种:

  • 将detailView分配给tableFooterView 属性。

  • 降低表格视图的高度并将detailView添加到self.view。

Not sure which behavior you're looking for but try one of these:

  • Assign the detailView to the tableFooterView property of the tableview on the second VC.

  • Reduce the height of the table view and add the detailView to self.view.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文