Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

alamarco

macrumors newbie
Original poster
Feb 14, 2010
4
0
Canada
Is it possible to bind buttons to an NSView and its variables? I've been trying for the past 4hrs, but I'm getting no where.

Background: I'm going through Cocoa Programming For Mac OS X Third Edition. The challenge is as follows: "Add the Boolean variables bold and italic to your BigLetterView. Add check boxes that toggle these variables. If bold is YES, make the letter appear in boldface; if italic is YES, make the letter appear in italics."

My problem arises from linking the bold and italic variables to the checkboxes. I've been trying through the Interface Builder by clicking on the checkbox (i.e. the bold checkbox) and then going to the Button Bindings in the Inspector. When I go to the Value section the view doesn't show up in the Bind to drop-down box.

If I create an object of the view it shows up in the Bind to drop-down box, but I now have two objects so toggle the button doesn't do anything to the other object. I've tried finding a way to link the objects, but couldn't find anything. I assumed from the start that this wasn't the way, but I've been trying anything.

I've confirmed that creating IBOutlet NSButton works, but that's not what the question is asking. By going through it in this manner, I can get the application working correctly, but obviously not using the boolean variables.

I'm not sure if any code is necessary since I assume the problem and solution will be found in the Interface Builder. If a screenshot of the Interface Builder or any code is necessary please ask and I'll post them right away. I'll refrain from posting them now so as to not clutter the topic with potentially wasteful information.

I appreciate any help. :)

edit: In my frustration I didn't even think of uploading a zip of the code. Warning, in the Interface Builder there are a few errors since I was fooling around. They aren't critical, but if I remove them they remove functionality like quiting, etc so I just left the errors there. Like stated above, I'm just trying to figure out a way to link the boolean variables in BigLetterView to the checkbox bindings.

Code
 
Okay I ended up getting the solution.

I ended up creating an an outlet for an instances of my class:
Code:
IBOutlet BigLetterView *blv;

Once I did this, I dragged out a new NSObject in Interface Builder and set it to BigLetterView. I then preceded to bind the value like so:

Code:
Bind to: Big Letter View (the NSObject I just created)
Model Key Path: blv.stringBold (the BOOL for bold)

Now for a follow up question, is this proper? Unless I'm misunderstanding the link, it seems to me like I created an unnecessary object by linking a new NSObject to BigLetterView with the outlet for an instance of itself.

Any insight to whether this was correct? I hate just going for an end result because I worry that my method of doing things was a memory hog.
 
Does your solution work, or does it work in that you can bind to your view?

My understanding from your description is you want to bind controls to a view, but what you did was create a new view as a top-level nib object and bound to that view. This view is just a floating view, not actually inside a window. This could work, but you'd have to write code to add this view to your window.

I believe the proper way to bind directly to a view is to create an instance of an NSObjectController the same way you created your NSObject. Then, connect the object controller's content outlet to your view instance in your window and bind controls to the object controller's selection (which would be the view).
 
My solution did work, but the problem with my solution was I was creating an unnecessary clone by creating an NSObject instead of an NSObjectController. Using your words, I had an extra object floating around, which I obviously did not want since it wouldn't be resource friendly.

I had looked at NSObjectController, but could not figure out how to properly link it. I was looking at the Class Identity, when I should of been looking at the content Outlet.

Thank-you for your response. I was able to implement it perfectly, and I now understand NSObjectController's a little bit better. Although the class is important if I'm working with other types of objects, the major key is the content Outlet.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.