Challenge: Number Pad


Well, the hint pretty much gives it away for this one. If you click on your .xib file for the DetailViewController, and then click on the UITextField for the value, you can easily set the keyboard type.

I wanted to do it programmatically, as well, so I set the keyboard type back to Default in the Inspector, and moved over to the ItemDetailViewController.m file, and in the viewWillAppear: method, added a single line of code (I had to do some digging in the Documentation just to find these properties). I ran my application again, and got the same result as I did when setting it via the Inspector. I’m not 100% confident that this is the BEST method to set the keyboard value, so if anyone else has a better place, by all means. Also, this book doesn’t really use the dot notation for accessing. I know Obj-C uses this, so I was able to leverage it here. I couldn’t really figure out the best way to set the keyboard type via [ ] messages.

I also tried first setting up a NotificationCenter and setting the valueField object as an observer to the UITextFieldDidBeginEditingNotification, but this solution is much much simpler.

[code] // Use filtered NSDate object to set dateLabel contents
[dateLabel setText:[dateFormatter stringFromDate:[possession dateCreated]]];

valueField.keyboardType = UIKeyboardTypeNumberPad;

// Change the navigation item to display name of possession
[[self navigationController] setTitle:[possession possessionName]];[/code]


Objective-C’s “dot notation” is avoided by the book because the notation obscures what’s really happening which can end up tripping beginners, not to mention the visual ambiguity with accessing C struct elements.

Dot notation translates directly to [ ] messages:

// SETTING a value
obj.value = 1;
[obj setValue: 1];  //identical to the statement above

// GETTING a value
NSLog( @"%d", obj.value );
NSLog( @"%d", [obj value] );  //identical to the statement above

Thus, setting the keyboard type can be done via:

[valueField setKeyboardType: UIKeyboardTypeNumberPad];
valueField.keyboardType = UIKeyboardTypeNumberPad;  //identical to the statement above