UIScrollViews 中的 UIViewControllers:我没有正确理解什么?
我将通过陈述我在应用程序中尝试执行的操作来开始我的问题:
我正在尝试构建一个文档“选择器”屏幕,类似于 iPad 上的页面或数字,如下所示:
我想要一个可以在其中放置子视图的 UIScrollView
;这些子视图可能有控件,例如页面/数字中的日期/名称“排序”分段控件,或者它们可能是文档的图像。子视图显示什么并不重要 - 我希望子视图成为支持 UIViewControllers
的视图。这就是我的困惑开始的地方。
看,我最初尝试使用 UITableView
构建我的文档选择器屏幕。由于超出这个问题范围的各种原因,这没有成功。作为这项工作的一部分,我创建了 UITableViewCell
子类(以及随附的 XIB),按照 本教程(在 iOS 4 中创建自定义 UITableViewCell)。我心想,这太棒了 - 我有可以在 UITableView
中显示的视图,并且有办法从这些视图中获取事件,就像这样 (sortTypeChanged< /code> 响应我的日期/名称分段控件中的选择更改):
@interface DocSortViewController : UITableViewCell {
}
-(IBAction)sortTypeChanged:(id)sender;
@end
这一切都很好 - 直到我意识到 UITableView
无法得到我想要的东西。所以我放弃了它,转而使用 UIScrollVIew
。然而,这就是我目前的知识让我失败的地方。自定义UITableViewCells
的“使用视图控制器/XIB视图”范例似乎不适用于UIScrollViews
。
我不知道如何创建一个单独的视图控制器和随附的 XIB,我可以在 UIScrollView
中加载和显示它们。尝试通过 UINib.instantiateWithOwner
加载 XIB 并添加视图控制器的 view
属性作为子视图会崩溃。我不知道为什么。
但令我担心的是,我似乎从根本上误解了视图控制器/视图的工作原理。尤其是与 UIScrollViews 结合使用时。
我缺少什么?有没有人尝试过做类似的事情?是否有推荐的最佳实践?如果您不应该将视图控制器/XIB加载到UIScrollViews
中,那么您应该做什么?如何从 UIScrollView
中的子视图上的控件获取事件?
编辑:我应该补充一点,我的问题的一部分是我通过 WPF 的视角来看待这个问题,其中使用 MVVM 模式,您可以通过将较小的视图组合起来来构建完整的视图视图,每个视图都有自己的支持视图模型。在 iOS 中复制这一点是否现实?还是真的不推荐?
I'll start my question by stating what I'm trying to do in my app:
I'm trying to build a document "picker" screen, similar to Pages or Numbers on the iPad, something that looks like this:
I'd like to have a UIScrollView
that I drop subviews in; those subviews might have controls on them, like the Date/Name "sort" segmented control in Pages/Numbers, or they might be images of documents. It doesn't really matter what the subviews display - what I'd like is for the subviews to be views with backing UIViewControllers
. This is where my confusion starts.
See, I originally tried to build my document picker screen by using a UITableView
. For various reasons beyond the scope of this question, that did not work out. As part of that effort, I created UITableViewCell
subclasses (and accompanying XIBs), as per this tutorial (Creating a custom UITableViewCell in iOS 4). I thought to myself, this is great - I have views that I can show in a UITableView
, and have a way to get events from those views, in a way such as this (sortTypeChanged
responds to a selection change in my Date/Name segmented control):
@interface DocSortViewController : UITableViewCell {
}
-(IBAction)sortTypeChanged:(id)sender;
@end
This was all well and good - until I realized UITableView
was not going to get me what I wanted. So I abandoned it, in favor of UIScrollVIew
. However, this is where my current knowledge fails me. The "use a view controller/XIB view" paradigm for custom UITableViewCells
does not seem to work for UIScrollViews
.
I cannot figure out how to create a separate view controller and accompanying XIB that I can load and display in my UIScrollView
. Attempts at loading the XIB via UINib.instantiateWithOwner
and adding the view controller's view
property as a subview crashes. I'm not sure why.
What concerns me, though, is that it seems I'm fundamentally misunderstanding how view controllers/views work. Especially when coupled with UIScrollViews
.
What am I missing? Has anyone tried to do something similar to this? Is there a recommended best practice for this? If you're not supposed to load view controllers/XIBs into UIScrollViews
, what are you supposed to do, then? How would you get events from controls on subviews residing in UIScrollView
?
Edit: I should add that part of my problem is that I'm looking at this through the lens of WPF, where, using the MVVM pattern, you'd build a full view by composing it of smaller views, each with their own backing view model. Is it realistic to replicate that in iOS? Or is it not really recommended?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 iOS 中,“视图控制器”是一个特殊的实体,通常控制整个屏幕的子视图(iPad 上的 UISplitViewController 和 UIPopoverController 是例外)。您链接的教程告诉您为 UITableViewCells 创建视图控制器 xib 是错误的。您应该做的是为 UITableViewCell 子类创建一个 xib,不涉及视图控制器。不过,乍一看,其余部分似乎或多或少是正确的。
您可以使用 UIScrollView 执行相同的操作,只是您没有方便的
tableView:cellForRowAtIndexPath:
方法来告诉您何时从 xib 创建子视图。如果没有您发布“崩溃”时出现的实际错误,我只能猜测,但我猜您作为instantiateWithOwner:options:
的所有者提供的任何对象与笔尖所期望的不匹配。您作为所有者传递的对象必须是在 nib 中指定所有者的任何类的实例。In iOS, a "view controller" is a special entity that usually controls an entire screen's worth of subviews (UISplitViewController and UIPopoverController on iPad are the exception). The tutorial you link is wrong in telling you to create a view controller xib for the UITableViewCells. What you should have done is create an xib for your UITableViewCell subclass, no view controller involved. The rest of it seems more or less correct at first glance, though.
You can do about the same thing with a UIScrollView, except that you don't have a handy
tableView:cellForRowAtIndexPath:
method to tell you when to create the subviews from the xib. Without your posting the actual error you get when it "crashes" I can only guess, but I guess whatever object you are providing as the owner forinstantiateWithOwner:options:
does not match what the nib expects. The object you pass as owner must be an instance of whatever class the owner is specified as in the nib.