TableView Delegate


#1

Thank you so much for this book!
It is helping me learn XCode for the first time.

(My son and husband have prodded me into making the shift from RealStudio )

2 Questions:

  1. Why is the not needed in the header file of SpeakLineAppDelegate in chapter 6 ?

  2. Is it possible to set the delegate of the tableView from code – how would that be done ?

Just curious (i.e. wanting to understand it a little better)
Thanks !

Judy


#2

Hi Judy,

Great to hear that the book is working for you. :slight_smile: As to your questions…

  1. Because we make the delegate connection using Interface Builder, and IB doesn’t actually check the protocols that a class conforms to when making a connection, we can skip it. It would be better to add it, but it’s not necessary in this case.

  2. You can definitely set the delegate programmatically. Because NSTableView has -setDelegate: and -delegate methods, we can just use those:

Note that this assumes that we have a property outlet on self that is connected to the table view, called tableView. Also, and this ties into your first question, if we have not added the protocol to our class’s @interface, we will get a warning as the compiler checks to see whether self conforms to the NSTableViewDelegate protocol.

Adam


#3

Yes!
Thank you so much for your explanations. They were a great help.

I had previously tried [_tableView setDelegate:self];
but I did it in the -init method (since the [_speechSynth setDelegate:self] is there).

Now I’ve done the same thing with the _tableView as an instance variable only, and also as a synthesized property (just to explore);
it works just fine either way, … but not from the -init method. When I placed the code in the "awakeFromNib"
or the “applicationDidFinishLaunching”, then it worked.

I will try to remember to use properties. I keep forgetting that there could be something in a setter or getter method code that is needed
and that it is not the same as accessing the variable directly. Maybe I should write it on my toilet paper. : )

Judy


#4

Right. Remember that the outlets will not be connected in -init. Only after -awakeFromNib is called are the outlets set and ready for use.

Adam