Adding buttons in 17.14


#1

I ran into a stumbling block at Figure 17.14.

It has us adding a button and making connections again, but I forgot how to do that to get the described result. I drag from the File’s Owner to the button, but I don’t see how to name the connection “assetTypeButton.” I only see a popup that says “Outlets - View.” I don’t think I set it in Attributes Inspector.

So I went to the index to look up button connections, but neither “button” nor “UIButton” appears in the index. I found “File’s Owner” in the index and it references Pages 137-140, but that already assumes knowledge of how to connect things.

I found the connection stuff at the very beginning of the book, but I can’t find or remember how to create a connection and have something named “assetTypeButton” as described in the diagram, and the beginning of the book is using a delegate and not the File’s Owner, so I can’t figure out what I’m supposed to do. I know it should be a simple procedure but I can’t find the answer using the index and TOC.


#2

Remember in the Navigation Controller chapter, how we had you open up the header file by option-clicking on it, next to the XIB file. Then, you would control-drag from the XIB file into the header file. So, this is asking you to do that.

Or, you can manually enter these outlets in the header file and connect them. Note that the text in the book says to “create and connect the outlets” in the diagram


#3

So I was able to figure that part out on my own :slight_smile:

I successfully created the IBAction from the button to my ItemDetailViewController.h and implemented the method in my .m file. I assumed we also needed an IBOutlet from ItemDetailViewController to our new button so that ItemDetailViewController can tell the button which asset type to display.

So my code compiles fine. I can run in the simulator, and if I click the button to add a possession, the app crashes and a break appears on the line

in ItemViewController.m. The inline error is

The console says something about an uncaught NSInternalInconsistencyException

My code compiled and ran fine before adding this button, so I know it’s something to do with that. When I was playing around with the code, I connected the new button to File’s Owner as the view outlet. If I ran it then, I could click to add a possession, I’d get the new view, but it was just blank. If I clicked here, I’d get the asset picker. Perhaps creating the IBOutlet the way I did was not correct. I read the chapter on Instruments and attempted to use them to see if maybe I had forgotten a release or released prematurely, but Instruments seems to be buggy and not functioning properly.

Help is appreciated.


#4

So I figured this one out, but I’m not sure if it’s something I did along the line, or if maybe there is a gap in the book (my money is on something I did).

Somehow the outlet that connected my File’s Owner (in this case, ItemDetailViewController) to my view got disconnected (not sure if disconnected is the right term).

So, previously, when I was setting this new button as the view, I was able to click to add a new possession, but all I was seeing was this blank button as my main view.

Connecting the view back to File’s Owner allows me to see the view.

However, I’ve discovered another issue (since I started writing this post, actually).

If I click through to the asset picker, all works fine. If I click cancel on my Item Detail View, the app crashes with a EXC_BAD_ACCESS inline error message at

of my main function. I believe this has something to do with an object being deallocated too early? Not sure though…


#5

Aaaand figured this one out, too.

SO a word to the wise on using the control-drag method of connecting up your .xib files: If you make the connection, keep in mind there is more code added than just to the header file and a method (in the case of an IBAction). Because I had tried twice to make assetTypeButton an IBOutlet (once today, once yesterday) I generated the same lines of code twice in my viewDidUnload: and dealloc methods. Thus, I was releasing the button too many times.

Problem solved!


#6

Got it. Thanks for the reminder, Joe (and I just used it in Ch. 28). :slight_smile: