Question

Example I:

if (textField.text = @"0", @"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @"10") { label.text = @"0"; }
if (textField.text = @"11", @"12", @"13", @"14", @"15", @"16") { label.text = @"10"; }
if (textField.text = @"17", @"18", @"19", @"20", @"21", @"22") { label.text = @"20"; }
if (textField.text = @"23", @"24", @"25", @"26", @"27", @"28") { label.text = @"30"; }

As you can see below, I have a UIStepper that adds in a text field one by one, but here is what I want to do with the label: Once I get to eleven every six steps up the label will add 10 points (Example I).

But I'm pretty sure I've done a terrible job, Can someone help me?

Was it helpful?

Solution

Use this:

int value = [textfield.text intValue];

then do your value checking with integer numbers. You could use modulo or any other arithmetic then. To return it to a string do this:

label1.text = [NSString stringWithFormat:@"%d",value];

If your textfield contains integer values then textfield.text (an nsstring) can become an integer using the int value above. Afterwards, it's easy to check in which case the value is. The simplest is using ifs like this:

 if (value <=10) {
  label.text = @"0";
 } else {
   if ( value > 10 && value <=16 ) {
    label.text = @"10";
   } else { if ( value > 16 and value <= 22) {
      label.text = @"20";
     } 
   }
 }

and so on. As I said, it's the simplest if..else construct and comparison.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top