UITextField Value Checking

Discussion in 'iOS Programming' started by dant19, Jun 8, 2011.

  1. dant19 macrumors member

    Joined:
    May 28, 2008
    #1
    I am currently writing my first application, and currently the user enters a value into a textfield, presses a button that checks the value entered, and if correct the program carries on.

    This is pretty standard functionality, and I want to improve the user experience so that the behaviour is as follows:

    1. User enters text
    2. As each character is entered, the app checks the entire value
    3. If correct the programme automatically continues.

    This means that there is no interaction by the user at all (except the input). This functionality is not suitable for every text input situation, but lends itself to my app.

    I'm not looking for a solution as such (I am new to programming, so want to learn and not just copy!), but I am looking for a point in the right direction.

    I have read the UITextField Class Reference and from that it looks as if the delegate is the way to go, is this correct?

    Also, would this be a complex piece of functionality to implement? (relative term sure, but is it 200 lines of code or 2?

    Thanks for any help!

    Dan
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    First, welcome to the exciting world of iOS programming. And thank you for such a well-explained first question. I, and I suspect others, appreciate it.

    Correct. How much do you know about setting the delegate property for a UITextField or delegation in general? Also, have a look at the UITextFieldDelegate protocol reference to perhaps get some idea what might help.

    I would say closer to 2 than 200.
     
  3. dant19 thread starter macrumors member

    Joined:
    May 28, 2008
    #3
    dejo, thank you for the great reply.

    Not too familier with delagates, I have a loose understanding about what they do, but have never used them. Now that I know I'm heading in the right direction I will look at them more closely.

    Thanks again.

    Dan
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    You're welcome. Don't hesitate to ask follow-on questions if you need more guidance or clarification.
     
  5. dant19 thread starter macrumors member

    Joined:
    May 28, 2008
    #5
    Just wanted to close this thread off incase somebody finds it with the same problem.

    Few points I got caught up on:

    I was using the UITextField, which has the delegate method textFieldShouldChangeCharactersInRange but it was always one character behind which wasn't useful when you want to "live" checking.

    I switched to UI TextView, customised it a bit to look like a text box and used the delegate method textViewDidChange and all was rosy.

    And that was it, was pretty simple to do in the end, and app works great.

    Thanks (again!!) dejo for the nudge in the right direction.
     
  6. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #6
    It's possible to use an NSNotification for textFieldDidChange with UITextField. I don't know why it's not a delegate method but it's not.

    Usually with validation of input you want the ability to reject an input before it happens, rather than reject it after. That's what textFieldShouldChangeCharactersInRange gives you. So if you want to prevent entering certain characters into the textfield you return NO from that method.

    On the desktop Cocoa has a pretty full featured way to automatically validate input. I forget all the details but there's a class that can be configured to accept/reject inputs based on lots of different criteria (float, int, range, etc.). I'm sure there's a design pattern name for this. Anyway, if you find yourself doing this a lot consider building one or more classes that can be configured and which will work via textFieldShouldChangeCharactersInRange.
     

Share This Page