Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS

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.