IF statement not triggering...

Discussion in 'iOS Programming' started by iAppleFan08, Mar 28, 2009.

  1. iAppleFan08 macrumors newbie

    Joined:
    Aug 26, 2008
    #1
    I've seem to have run into another problem...

    It's actually something I'm kinda confused with and I'm not sure if I'm even doing it right. I'm trying to get something to happen with an IF statement, but the part that isn't working is:

    if (textBox.text != nil)
    {
    message.hidden = NO;
    }
    else
    {
    message.hidden = YES;
    button.hidden = NO;
    }

    The IF statement doesn't trigger. It does the (textBox != nil) result regardless if there is anything in it or not. I double and tripple checked connections, and there are no errors or warnings. Am I missing something simple again or is there another way to do this...?
     
  2. eddietr macrumors 6502a

    Joined:
    Oct 29, 2006
    Location:
    Virginia
    #2
    What exactly do you mean by "if there is anything in it"? Do you mean you are expecting textBox to be nil if the user hasn't put any content in the box?

    What you are checking is if the textBox variable itself (the actual pointer assuming textBox is a UITextField* or something) is equal to nil.
     
  3. iAppleFan08 thread starter macrumors newbie

    Joined:
    Aug 26, 2008
    #3
    I am checking if the pointer has anything in it. I have an IBOutlet set as well as the @property and @synthesize that goes with it. I checked the .xib file and the pointer is connected to the text box.
     
  4. Guiyon macrumors 6502a

    Joined:
    Mar 19, 2008
    Location:
    North Shore, MA
    #4
    Assuming that textBox is an NSTextField* and it is properly linked, you're code is doing exactly what it's being told to: making sure the textBox pointer is not nil! Judging from you're description you're more interested in the contents of the NSTextField rather than the address that the pointer points to so I'm assuming you're looking for something like:
    Code:
    if( [textBox.text length] > 0 ) {
    ...
    } else {
    ...
    }
    I believe I have the stringValue method right; My Cocoa/Objective-C is a bit rusty.

    Edit: Nope, I don't. UIKit uses the 'text' property of the UITextField, not the stringValue method
     
  5. eddietr macrumors 6502a

    Joined:
    Oct 29, 2006
    Location:
    Virginia
    #5
    Well, it's important to understand what is meant by the pointer having something in it.

    If you've connected the outlet to a UITextField in IB, then the pointer always has something in it. It has the address of the UITextField.

    If what you are really checking for is whether the UITextField itself has any content in it, then that's something else all together. If that's the case, then you want to check the text property of the UITextField, which is an NSString*.

    Hope that helps.
     
  6. iAppleFan08 thread starter macrumors newbie

    Joined:
    Aug 26, 2008
    #6
    Warning: 'UITextField' may not respond to '-stringValue'
     
  7. eddietr macrumors 6502a

    Joined:
    Oct 29, 2006
    Location:
    Virginia
    #7
    You can use the 'text' property of UITextField to get the string that it is displaying.
     
  8. Guiyon macrumors 6502a

    Joined:
    Mar 19, 2008
    Location:
    North Shore, MA
    #8
    IIRC, on the iPhone you're looking for the 'text' property, not the stringValue method.
     
  9. iAppleFan08 thread starter macrumors newbie

    Joined:
    Aug 26, 2008
    #9
    sorry... I messed up with what I put up there. The original code I had was:

    if (textBox.text != nil)

    I edited it.
     
  10. Guiyon macrumors 6502a

    Joined:
    Mar 19, 2008
    Location:
    North Shore, MA
    #10
    That doesn't quite work either since the returned string may be an NSString of length 0, which is a non-nil and valid object.

    Try:
    Code:
    if( (textBox.text != nil) && ([textBox.text length] > 0) )
    The "!= nil" covers the default case (see Apple's documentation) and the length part checks to make sure there is at least something in the string.
     
  11. iAppleFan08 thread starter macrumors newbie

    Joined:
    Aug 26, 2008
    #11
    That worked! Thank you!
     

Share This Page