Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


Setting keyboard type programmatically in Xcode

I have created an IBOutlet with multiple text fields and would like to change the keyboard type to the number pad. I have done so within the Utility pane but it does not change the keyboard type when I run the simulator. I have tried multiple methods including the below example. Any suggestions and also where should I put it?

nameofIBOutlet.keyboardType = UIKeyboardType.NumberPad

1 Answer

I added a text field to my view and changed the keyboard type to "number pad" in the attributes inspector. I then ran the app in the simulator and the number pad was displayed when the text field was selected. I'm not sure why this is not working for you.

I then tried a second approach in which I created an IBOutlet for the text field (note: I changed the keyboard type back to default in the attributes inspector). I then created an IBAction for the text field using the "Editing did begin" event. In this function I added the code to change the keyboard type. This also works in the simulator.

    @IBAction func testEdit(sender: AnyObject) {
        testTextField.keyboardType = .NumberPad

Strangely it just randomly started working one day.