UIViewControllers within UIScrollViews: what am I not understanding correctly?
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?
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 for instantiateWithOwner: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.
精彩评论