Monday, July 28, 2008

Hillegass Challenge: count letters


On p. 90, we're instructed to build an app with a Text Field where the user enters a string, and another one which displays the character count of the string. I did this using bindings. The Text Fields are bound to variables "textField" and "count" and we implement setTextField_ with a check for string value of None, which is what we get when we delete the last character of a string in the Text Field. Continuously update value is checked in the binding.

class PyStrLenAppDelegate(NSObject):
textField = objc.ivar('textField')
count = objc.ivar('count')

def setTextField_(self,value):
NSLog("setTextField_ %s" % value)
if not value: x = '0'
else: x = value.length()
self.setCount_(x)

def setCount_(self,value): self.count = value