Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

iAppleFan08

macrumors newbie
Original poster
Aug 26, 2008
25
0
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...?
 

eddietr

macrumors 6502a
Oct 29, 2006
807
0
Virginia
It does the (textBox != nil) result regardless if there is anything in it or not.

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.
 

iAppleFan08

macrumors newbie
Original poster
Aug 26, 2008
25
0
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.
 

Guiyon

macrumors 6502a
Mar 19, 2008
771
4
Cambridge, MA
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
 

eddietr

macrumors 6502a
Oct 29, 2006
807
0
Virginia
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.

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.
 

iAppleFan08

macrumors newbie
Original poster
Aug 26, 2008
25
0
sorry... I messed up with what I put up there. The original code I had was:

if (textBox.text != nil)

I edited it.
 

Guiyon

macrumors 6502a
Mar 19, 2008
771
4
Cambridge, MA
sorry... I messed up with what I put up there. The original code I had was:

if (textBox.text != nil)

I edited it.

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.
 

iAppleFan08

macrumors newbie
Original poster
Aug 26, 2008
25
0
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.

That worked! Thank you!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.