Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 10-11-2011, 04:41 AM   #1 (permalink)
Registered Member
 
Join Date: Sep 2011
Posts: 15
gogoraj is on a distinguished road
Smile If() statement text boxes

If I wanted a program to use a certain method only if certain text boxes are filled, how would I do that?

I.e. if text box x and y and z have (any) values then do this
else if text box a and b and c have (any) values then do this.

Last edited by gogoraj; 10-11-2011 at 04:41 AM. Reason: *spelling
gogoraj is offline   Reply With Quote
Old 10-11-2011, 06:45 AM   #2 (permalink)
Registered Member
 
Join Date: Nov 2010
Posts: 1,106
Meredi86 is on a distinguished road
Default

Create text boxes.
Hook up IBOutlets for the text boxes (ties the text box to a "name")
write out method for the text boxes
create if statements:
Code:
if (textField == self.textFieldName) {
if (self.textFieldName == nil) {
execute statement
}
}
Meredi86 is offline   Reply With Quote
Old 10-11-2011, 08:18 AM   #3 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

Besides checking for nil you should also check if it equals @"" (empty string), maybe even trim the textfield's string before checking.
baja_yu is offline   Reply With Quote
Old 10-11-2011, 11:02 AM   #4 (permalink)
Registered Member
 
Join Date: Sep 2011
Posts: 15
gogoraj is on a distinguished road
Default

Quote:
Originally Posted by Meredi86 View Post
Create text boxes.
Hook up IBOutlets for the text boxes (ties the text box to a "name")
write out method for the text boxes
create if statements:
Code:
if (textField == self.textFieldName) {
if (self.textFieldName == nil) {
execute statement
}
}
So if I wanted say 2 text boxes to have values how would I put that in one?
e.g. if (textField == self.textFieldName)
AND (textField2 == self.textFieldName2) {
etc.
gogoraj is offline   Reply With Quote
Old 10-11-2011, 11:22 AM   #5 (permalink)
Registered Member
 
Join Date: Nov 2010
Posts: 1,106
Meredi86 is on a distinguished road
Default

Google is great at searching for the operators that take the place of AND/OR.

AND = &&
OR = ||

so
Code:
if (self.TextFieldA == nil) && (self.textFieldB == nil) {
statements
}
would mean that textFieldA must = nil AND textFieldB must = nil for the statement to execute
Meredi86 is offline   Reply With Quote
Old 10-11-2011, 11:26 AM   #6 (permalink)
Registered Member
 
Join Date: Sep 2011
Posts: 15
gogoraj is on a distinguished road
Default

Quote:
Originally Posted by Meredi86 View Post
Google is great at searching for the operators that take the place of AND/OR.

AND = &&
OR = ||

so
Code:
if (self.TextFieldA == nil) && (self.textFieldB == nil) {
statements
}
would mean that textFieldA must = nil AND textFieldB must = nil for the statement to execute
Yes it would. In fact there are three things that have to != nil.
Basically I want it to detect out of 5 text boxes, which ones actually have values entered and to choose an IBAction accordingly.
Where would I position my If statements in my code?
gogoraj is offline   Reply With Quote
Old 10-11-2011, 09:16 PM   #7 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Jan 2010
Location: Issaquah, WA
Age: 42
Posts: 1,244
dljeffery is on a distinguished road
Default

Quote:
Originally Posted by gogoraj View Post
Yes it would. In fact there are three things that have to != nil.
Basically I want it to detect out of 5 text boxes, which ones actually have values entered and to choose an IBAction accordingly.
Where would I position my If statements in my code?
Please show your code that doesn't work.
__________________
Recall It! Tag your notes. Tag your photos. Tag your thoughts. Tag your life.

Recall It! for iPad

http://www.dljeffery.com
dljeffery is offline   Reply With Quote
Old 10-12-2011, 01:08 AM   #8 (permalink)
Registered Member
 
Join Date: Sep 2011
Posts: 15
gogoraj is on a distinguished road
Default

Quote:
Originally Posted by dljeffery View Post
Please show your code that doesn't work.

if (txtUvalue != nil && txtvValue != nil && txtTvalue!= nil )
{- (IBAction) calculateSvalue1: (id) sender
{
float uValue = [txtUvalue.text floatValue];
float vValue = [txtVvalue.text floatValue];
float tValue = [txtTvalue.text floatValue];
float sValue = 0.5 * (uValue + vValue) * tValue;

lblSvalue.text = [NSString stringWithFormat:@"%0.002f", sValue] ;
}
}


I keep getting an error 'expected (' before the if.
gogoraj is offline   Reply With Quote
Old 10-12-2011, 03:05 AM   #9 (permalink)
Registered Member
 
Join Date: Nov 2010
Posts: 1,106
Meredi86 is on a distinguished road
Default

Code:
if (txtUvalue != nil && txtvValue != nil && txtTvalue!= nil )
{- (IBAction) calculateSvalue1: (id) sender
{ 
float uValue = [txtUvalue.text floatValue];
float vValue = [txtVvalue.text floatValue];
float tValue = [txtTvalue.text floatValue];
float sValue = 0.5 * (uValue + vValue) * tValue; 

lblSvalue.text = [NSString stringWithFormat:@"%0.002f", sValue] ;
}
}
Use these tags to surround your code - makes it easier to read [ code ] [/ code ] (but without the white space).

Basically you need to treat each evaluation as a separate one:
Code:
if ((txtUvalue != nil) && (txtvValue != nil) && (txtTvalue!= nil))
{- (IBAction) calculateSvalue1: (id) sender
{ 
float uValue = [txtUvalue.text floatValue];
float vValue = [txtVvalue.text floatValue];
float tValue = [txtTvalue.text floatValue];
float sValue = 0.5 * (uValue + vValue) * tValue; 

lblSvalue.text = [NSString stringWithFormat:@"%0.002f", sValue] ;
}
}
This is one of my OR setups:
Code:
if (([elementName isEqualToString:@"status"]) || ([elementName isEqualToString:@"message"]) || ([elementName isEqualToString:@"sessionKey"]))
The [ brackets are for the syntax within the evaluation - and your code shouldn't require these. but it shows you generally how they are made up

Last edited by Meredi86; 10-12-2011 at 03:06 AM. Reason: few typos
Meredi86 is offline   Reply With Quote
Old 10-12-2011, 08:27 AM   #10 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by gogoraj View Post
if (txtUvalue != nil && txtvValue != nil && txtTvalue!= nil )
{- (IBAction) calculateSvalue1: (id) sender
{
float uValue = [txtUvalue.text floatValue];
float vValue = [floatValue];
float tValue = [txtTvalue.text floatValue];
float sValue = 0.5 * (uValue + vValue) * tValue;

lblSvalue.text = [NSString stringWithFormat:@"%0.002f", sValue] ;
}
}


I keep getting an error 'expected (' before the if.

Ok, several things.

You say you keep getting 'expected (' before the if. If you're getting an error before the if statement, you need to post the code that appears before the if statement.

Next: I think you are getting some bad advice about how to check if a field is blank. I'm fairly certain that you will never get nil back from the text property of a text field. You will get an empty string instead.

So, ytxtVvalue.textour if statement should read:

Code:
if ([txtUvalue.text length] != 0 && 
  [txtvValue.text length] != 0 &&
  [txtTvalue.text length] != 0)
Second, your code inside the "{" and "}" brackets is the declaration of an IBAction method. That is not valid syntax. You need to put regular statements inside the brackets, not a method declaration.

When do you check to see if these fields are blank and update your result field? On a button push?

If it is on a button push, your code might look like this:

Code:
- (IBAction) buttonAction: (id) sender;
{
  if ([txtUvalue.text length] != 0 && 
    [txtvValue.text length] != 0 &&
    [txtTvalue.text length] != 0)
  {
    float uValue = [txtUvalue.text floatValue];
    float vValue = [txtvValue.text floatValue];
    float tValue = [txtTvalue.text floatValue];
    float sValue = 0.5 * (uValue + vValue) * tValue;  
    
    lblSvalue.text = [NSString stringWithFormat:@"%0.002f", sValue] ;
  }
}
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 10-12-2011, 08:47 AM   #11 (permalink)
Registered Member
 
Join Date: Nov 2010
Posts: 1,106
Meredi86 is on a distinguished road
Default

Quote:
Next: I think you are getting some bad advice about how to check if a field is blank. I'm fairly certain that you will never get nil back from the text property of a text field. You will get an empty string instead.
Kind of two reasons i had gone down that line:

1, Any advice from any forum should get checked over first unless there are a good few of the top posters agreeing on the issue, so here was a way of building in a mistake that could get found and explained by someone else - just as Duncan has and to stop the idea of getting everything done for you .

2, i have had text fields before that when nothing has been entered they have saved a nil value. this was one of the things that caused me a big headache in getting my web service to tie in properly, every time i sent something there would be nil's there from the lack of user input, which in turn later returned the data and displayed "nil" in a bunch of fields - not to snazzy to look at!!

So in my app i now check the text fields like this:
Code:
if(textField != nil)
            {
            self.savedName = textField.text;
            }
in order to prevent saving "nil" as the "string" that was entered. Maybe its not the most elegant of solutions, but it is one that has been working 100% for me. Looking at Duncan's post though i would preferably switch to his method (app is in review so a bit late now) as it looks slightly more robust than my solution.
Meredi86 is offline   Reply With Quote
Old 10-12-2011, 01:49 PM   #12 (permalink)
Registered Member
 
Join Date: Sep 2011
Posts: 15
gogoraj is on a distinguished road
Smile

Quote:
Originally Posted by Duncan C View Post
Ok, several things.

You say you keep getting 'expected (' before the if. If you're getting an error before the if statement, you need to post the code that appears before the if statement.

Next: I think you are getting some bad advice about how to check if a field is blank. I'm fairly certain that you will never get nil back from the text property of a text field. You will get an empty string instead.

So, ytxtVvalue.textour if statement should read:

Code:
if ([txtUvalue.text length] != 0 && 
  [txtvValue.text length] != 0 &&
  [txtTvalue.text length] != 0)
Second, your code inside the "{" and "}" brackets is the declaration of an IBAction method. That is not valid syntax. You need to put regular statements inside the brackets, not a method declaration.

When do you check to see if these fields are blank and update your result field? On a button push?

If it is on a button push, your code might look like this:

Code:
- (IBAction) buttonAction: (id) sender;
{
  if ([txtUvalue.text length] != 0 && 
    [txtvValue.text length] != 0 &&
    [txtTvalue.text length] != 0)
  {
    float uValue = [txtUvalue.text floatValue];
    float vValue = [txtvValue.text floatValue];
    float tValue = [txtTvalue.text floatValue];
    float sValue = 0.5 * (uValue + vValue) * tValue;  
    
    lblSvalue.text = [NSString stringWithFormat:@"%0.002f", sValue] ;
  }
}
Thanks, this has solved it. Most of my values are working perfectly now!
The only problem now is trying to solve a quadratic in the IBAction...
But that will come later. Thanks!
gogoraj is offline   Reply With Quote
Reply

Bookmarks

Tags
boxes, if(), statement, text, values

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 403
19 members and 384 guests
13dario13, 7twenty7, buggen, eski, EvilElf, glenn_sayers, HemiMG, jarv, LunarMoon, morterbaher, n00b, pbart, Pudding, QuantumDoja, sacha1996, Sami Gh, UMAD, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,673
Threads: 94,122
Posts: 402,906
Top Poster: BrianSlick (7,990)
Welcome to our newest member, morterbaher
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 05:23 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0