>> Also, it’s not clear what the correct segue type ought to be in your case. 
>> You said nothing about navigation controllers or split controllers, so your 
>> master/detail relationship is informal, not the one built into iOS, I think.
> 
> I tried a navigation controller, but it made its own scene. I have no idea 
> how to use it; I wanted to get this master/detail thing working before moving 
> to a more advanced topic. Perhaps navigation controllers aren't advanced at 
> all, but from the outline table, it's in no way obvious how to use them. 
> Things online just keep telling me to embed my table or table controller in a 
> navigation controller, then change some aspects of that controller, and that 
> seems to be the end of it. I’ll look more into it, but as I said, they seemed 
> more complex so I thought I'd master the basics first.

When you are building an app with a table view, and tapping on the cell should 
then show a detail view, you usually want a navigation controller (there are 
other cases like a split view controller, but the navigation controller is the 
simplest one). 

A navigation controller is a container view controller and contains other view 
controller that display (most of) the actual interface of your application. It 
also manages the navigation between different view controllers in your app. I 
understand where you are coming from when trying to understand the different 
parts step by step. However, navigation controllers are quite essential to apps 
with a hierarchical navigation (and almost all apps with a table view have a 
hierarchical navigation) and when using them in the simple cases they are also 
quite simple to use and understand. I recommend, that you take a look at the 
UINavigationController class reference:

https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UINavigationController_Class/index.html
 
<https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UINavigationController_Class/index.html>

You don’t need to read all of it, just read the first view paragraphs before 
the heading „Navigation Controller Views“ and then continue with the paragraphs 
below that heading up to „Updating the Navigation Bar“. You don’t have to 
update the navigation bar just yet (all the updates you need will happen 
automatically, like changing the title). so you can ignore everything after 
that for now. This will hopefully give you an idea what a navigation controller 
is about and what it does. There are also great illustration in the class 
reference, but I’m afraid they won’t help you much as a Voice Over user?

If the explanations are not clear without the illustrations (e.g. you are not 
sure what the navigation view is), please ask again and I’ll try to describe it.

I have just set up an example table view controller with a segue from the cells 
to a detail view controller but without a navigation controller, to see what 
will happen. The resulting app does show the detail view when tapping on a cell 
(so that part should still work) but there is no way to go back. So you 
definitely want a navigation controller. ;-)


To try to help you, I’ll describe how to set up such a table view with a detail 
view from scratch.
I’ll try to setup a really simple example step by step and hope you can follow 
along and then maybe see what went wrong for you:

1. Create a new project and choose the template „Single View Application“ 
(because it is the smallest template project without lots of unnecessary stuff)
2. Uncheck „Use Core Data“ to make it simpler
3. You should now have a project with an AppDelegate class file and a View 
Controller class file (depending on Swift or Objective-C you have one or two 
files for each ;-) ) and a Main.storyboard file (some other files are there as 
well, but not interesting for now.

Let’s clean up a bit first and get rid of the stuff we don’t need.
4. Delete the „ViewController“ file(s) („ViewController.h“ and 
„ViewController.m“ in my case, it should be just one for a Swift project). 
5. Select the Main.storyboard file. It should have just one „View Controller“ 
scene
6. Select the „View Controller“ scene and delete it (e.g. using the „delete“ 
key)

Now you have a blank storyboard and we are ready to start.

7. From the object library drag a „Navigation Controller“ onto the Storyboard
        This will create two scenes, one „Navigation Controller“ scene and one 
„Root View Controller“ scene and a „relationship“ between them called „root 
view controller“. This symbolizes that the view controller in the „Root View 
Controller“ scene is the root view controller of the navigation controller, 
i.e. the first view controller, the navigation controller will display. 
        We don’t need to do anything about the navigation controller, all we 
need is that it contains our „Root View Controller“, which is already set up.
        Examine the „Root View Controller“ scene more closely. You will find 
that the view controller is already a UITableViewController and the scene 
already contains a table view with „dynamic“ cells and already contains a first 
table view cell. The table view cell, does not have any title or anything 
because it’s of style „Custom“. To change that (and make it easier to identify 
the cell in the running app, especially with VoiceOver) select the cell and in 
the „Attributes Inspector“ choose Style „Basic“ from the popup-menu. 

8. While you are in the Attribute Inspector enter a „reuse identifier“ in the 
„Identifier“ field. This is a simple string that allows you to find/create that 
kind of cell in your code. I choose „SnowflakeCell“ for mine, but you can 
choose anything that fits its purpose. 

9. From the Object library choose a „View Controller“ and drag it onto the 
canvas. This creates a new scene which is named „View Controller Scene“. 
10. Select the view controller in that scene and in the attributes inspector 
enter „Detail“ in its „Title“ field. This should also rename the scene to 
„Detail Scene“, which is a more appropriate description.
11. As you did before, choose the „Connections Inspector“ to display the 
different possible connections. 
12. In the „Presenting Segues“ section, drag from the „show“ connection to the 
cell (the „SnowflakeCell“) in the „Root View Controller“ scene. Please choose 
„show“ here and not any of the others, especially not „show detail“. In the 
popup that appears after dragging to the cell, choose „selection“, which means 
the segue should be executed when the cell is selected.


Now we are almost done in the storyboard. However, I forgot one thing that’s 
always easy to overlook with storyboard: We forgot to choose the initial view 
controller. So:
13. Select the navigation controller again and in the Attributes Inspect check 
the „Is Initial View Controller“ checkbox.
Save the storyboard.


Now our storyboard is setup correctly, all we need to do is make sure that we 
actually display some cells. To do that we need to write some code for our 
table view controller. 

14. Create a new file. Choose „Cocoa Touch Class“ from the template selection.
16. In the „Subclass of:“ combobox enter „UITableViewController“. This will 
also enter „TableViewController“ in the „Class:“ field above.
17. Choose a name for you class, I just prefix the proposed 
„TableViewController“ with a „My“ to get „MyTableViewController.
18. Click „Next“ and choose a location for the new file

You will now have a new file with an example implementation of a table view 
controller. From now on, I’ll assume you use Objective-C. If you don’t, I hope 
you will be able to translate my explanations yo your Swift use-case. 
Otherwise, ask. ;-)

19. Delete the „numberOfSectionsInTableView:“ method. If it’s not implemented 
the default is „1 section“ which is fine for our use-case and it simplifies the 
code
20. In the „tableView:numberOfRowsInSection:“ method simply return „3“ so we 
will have some cells to play with. You can remove the "#warning Incomplete 
implementation, return the number of rows“
21. Below that method, remove the /* and */ around the 
„tableView:cellForRowAtIndexPath:“ method.
22. The implementation of the method is almost done already. In the first line 
replace the @„reuseIdentifier“ placeholder with your cell identifier. I my case 
that was @“SnowflakeCell“.
23. Add a line before the „return cell“ statement to set the title of a cell. I 
recommend:

        cell.textLabel.text = [NSString stringWithFormat:@„Cell %d", 
(int)indexPath.row];


Now we just need to go back to the storyboard and tell it to use our table view 
controller.

24. In the Main.storyboard, select the table view controller in the  „Root View 
Controller“ scene.
25. In the Identity Inspector in the „Class“ field enter the name of your table 
view controller class. In my example that was „MyTableViewController“.


Now you should be able to run your app and have a working table view with 3 
cells with titles „Cell 0“ to „Cell 2“. Tapping on any of the cells pushes the 
„Detail“ view controller when you tap on a cell. 

Note that you do not need to implement any methods, except for 
tableView:numberOfRowsInSection: and tableView:cellForRowAtIndexPath: to get it 
to work for that state. prepareForSegue: is only needed to configure the detail 
view controller but not needed to make the segue itself work. 



I hope I was able to describe all the steps in a VoiceOver compatible fashion. 
If any of it fails, tell me which and I’ll try again. 



If you still can’t get it to work it might also be useful if you can upload 
your project (assuming it’s an example project you are playing with) so we can 
check which connection might be missing.


Good luck and have fun playing with Xcode and its Interface builder. ;-)


Joachim
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list      ([email protected])
Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/xcode-users/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to