PDA

View Full Version : Using math (division?) in cocoa bindings... iProcrastinate Question!




Littleodie914
Sep 13, 2006, 10:17 PM
Hey guys, after a long break (well not really break, I've been swamped in college stuff) I'm back working on iProcrastinate. One thing a buddy of mine suggested is putting a percentage completed column in the main window of the app that does some simple division based on the number of steps you've completed over the number of total steps. For example, if you're writing a paper and you've completed 3 of the 4 steps you set up, it would read 75%. This value should automatically update based on the number of steps, but I'm having trouble with the math.

How can I set up the model key path in a way that would do this division for me? I made small progress when I bound the column to the @count property of the ArrangedObjects controller key of my Steps NSArrayController. What this did was just changed the percentage value based on how many steps there were. (3 steps = 3%, 7 steps = 7%) This obviously isn't what I was going for, but was a step in the right direction. (In that it automatically updated when the number of steps increased or decreased.) Any ideas on how to implement some type of mathematics in this column? Thanks so much for all your help guys, I wouldn't be doing any of this if it wasn't for you. :o :)



HiRez
Sep 13, 2006, 10:29 PM
Probably the easiest way is to implement a -(float)pctDone method in your controller class that just feeds you a single number to bind, or even -(NSString*)pctDoneString, where you could format it beforehand to a certain number of decimal points or whatever. I usually end up with a bunch of methods to do things like this which I group as "Bindings Support". There may be an easier way to do it directly in IB, I dunno. The formatting in the bindings system seems kind of goofy to me, why they didn't use standard printf formatting I have no idea.

Littleodie914
Sep 14, 2006, 06:42 AM
Probably the easiest way is to implement a -(float)pctDone method in your controller class that just feeds you a single number to bind, or even -(NSString*)pctDoneString, where you could format it beforehand to a certain number of decimal points or whatever. I usually end up with a bunch of methods to do things like this which I group as "Bindings Support". There may be an easier way to do it directly in IB, I dunno. The formatting in the bindings system seems kind of goofy to me, why they didn't use standard printf formatting I have no idea.Alright, that's along the lines of what I was thinking about doing... How would I go about getting a value from a method in the bindings panel though? Like what would I put in the Model Key Path, in the Controller Key, etc. I don't have my Cocoa stuff handy at the time being, a friend of mine's borrowing it. :rolleyes:

devman
Sep 15, 2006, 12:08 AM
another idea could be your own nsvaluetransformer. then just set it in the bindings part of the ib inspector in the value transformer combo box. it's a fine line though whether this is putting too much logic in the nib. if you go this way remember to register your value transformer in your app delegate initialize.

Littleodie914
Sep 19, 2006, 06:45 PM
Bah... I hate to be a stickler, but I still can't figure this thing out! Maybe I don't know enough about custom bindings, but so far I have no idea how to feed a value from a method in the code to the actual bindings controller... Crazy stuff! :eek:

velocityofjack2
Sep 19, 2006, 08:37 PM
I think that the best way to implement this is by using key value observing.

First you'd have to sub-class NSManagedObject. This tutorial can help you out with that: http://developer.apple.com/cocoa/coredatatutorial/

Then read up on KVO at: http://developer.apple.com/documentation/Cocoa/Conceptual/KeyValueObserving/index.html

Basically, you'd set up a new key, such as percentageComplete, in the main assignment object, fill the accessor with the logic to do the division, which is dependent on the number of steps for a particular assignment and the number of those steps completed.

I hope this helps you work out the solution. Good Luck.