views:

274

answers:

1

Hi - first time asker, long-time lurker.

I am trying to create an iPhone view that has a date/time picker on the bottom half of the screen, and a grouped, single-section, four-row table view on the top half of the screen (almost identical to the one Apple shows in Fig. 2-4 of their View Controller Programming Guide (but then never goes on to explain).

Conceptually, I think I understand that what I need is a main view with a pair of subviews - one for the picker, and one for the table view. I'm pretty sure I can make the picker function once I have it on-screen, and I'm pretty sure I can make the table view function too. What I can't for the life of me figure out is how, programmatically speaking, to get the two views onto the screen simultaneously. I can lay it out perfectly in Interface Builder, but then it all goes to hell when I switch to Xcode...the view appears with the picker, but no table view.

Thanks, in advance, for any help you can offer.

A: 

the view appears with the picker, but no table view.

If the table view isn't assigned a data source, then it has nowhere from which to populate itself, and so it may give you the appearance of "not being there".

Have you created a view controller for this view? Is it a subclass of UITableViewController, or does it at least implement the UITableViewDelegate/UITableViewDataSource protocols? Is it set as the File's Owner in the .xib? And has it been assigned (using the connections) as your table view's delegate and dataSource?

In your .xib, are both the UITableView and the UIPickerView subviews of a top-level UIView, which is connected to your view controller's view property (i.e. for the File's Owner)?

UIView
+
|
+---UITableView
|
+---UIPickerView
Shaggy Frog
Until I get it figured out, I'm using the View-Based app template. In appDelegate's viewDidFinishLaunchingWithOptions:, I've added the following: CGRect smallTableViewRect = CGRectMake(0,0,320,240); UITableView *smallTableView = [[UITableView alloc]initWithFrame:smallTableViewRect]; CGRect pickerViewRect = CGRectMake(0,240,320,240); UIPickerView *picker = [[UIPickerView alloc]initWithFrame:pickerViewRect]; [window addSubView:smallTableView]; [window addSubView:picker]; [window makeKeyAndVisible]; This results in a table view background on top, and a black rectangle on bottom.
Andy
Sorry - I can't figure out how to format the code using the "Add Comment" box.
Andy
Edit your original post instead of commenting in this fashion.
Shaggy Frog
I would not put that code in the app delegate as a matter of good practice. You should be creating your views either (1) in Interface Builder, or (2) as .m files inside Xcode, and then you should be associating your views with a view controller. And it's *that* view controller's `view` that you should be adding as a subview to the window.
Shaggy Frog
Okay, but my original problem/question remains: How is that done, exactly? I can generally use a combination of Apple's docs, the semi-ridiculous stash of iPhone reference books I've purchased, and online tutorials and samples to accomplish what I want (my app is currently a mostly functional Core Data-based utility app with five or six working views / view controllers.But I cannot for the life of me figure this particular view out. Any specific help would be appreciated.
Andy
Are you asking "how do I create a view"? You can create one in Interface Builder as a .xib, or you can create a UIView subclass in code with Xcode, or both.
Shaggy Frog
Andy
When you open your .xibs in IB, do yourself a favour and click on the "list" mode (instead of using the default "icon" view mode). That will show you a hierarchy of the views in your app. You can drag-and-drop components from the Library tool window into your .xib window. What you want is to create a view hierarchy with a UIView at the root that has two children: a UITableView and a UIPickerView. Just like my diagram above. You then connect the various views to the appropriate outlets in File's Owner.
Shaggy Frog
Okay, then I must not be too far off the actual solution; that is exactly what I did the first time around, when I got the "expected a table view but didn't get one" error. So, if I understand you, what I need to do is create a DualViewController with two subviews - one table and one picker - and then push the controller at the appropriate time. Apparently what I've missed is the connecting-it-all-up part between IB and Xcode - any idea what would cause the error above? Also, the view controller - should it be just a plain UIViewController, or something more specialized? Thanks for the help!
Andy
The "expected a table view but didn't get one" error makes me wonder if your underlying view controller is subclassing `UITableViewController` -- if it is, subclass `UIViewController` and implement the `UITableViewDelegate` and `UITableViewDataSource` protocols directly.
Shaggy Frog