Search code examples
objective-ccocoa-touchios4xcode4

conditional formatting UI


The app I'm working on is a calculator basically. What i want to do is to is set up something so that on the results screen if the answers all = 60 or higher a big green check (an image I'll place in) appears and some text above or below that says you passes. If not I want a red X to appear that says you fail.

I know this should be simple I just don't know where to look to get the answers. If someone could point me in the right direction with a link or something so i can read up on this that would great. because i have tried looking but i just don't know what to look for to be honest.


Solution

  • So you basically just need to get the text from the UITextField as an integer like so:

    int sumOfFields += [[myUITextField text] intValue];
    

    You would do that for all of your fields. Then you need to check if it's greater than or equal to 60 or not:

    if (sumOfFields >= 60) {
        [self setValidationUI:TRUE];
    }
    else {
        [self setValidationUI:FALSE];
    }
    

    Where setValidationUI is a method that takes a boolean value that determines wether it should show the big green checkmark or a bit red X like so:

    -(void)setValidationUI:(BOOL)isValid {
        if (isValid) {
            [resultsImage setImage:[UIImage imageNamed:@"checkmark.png"]];
            [resultsTextField setText:@"You Passed!"];
        }
        else {
            [resultsImage setImage:[UIImage imageNamed:@"BigUglyRedX.png"]];
            [resultsTextField setText:@"You Fail..."];
        }
    }
    

    header file

    - (void)setValidationUI:(bool)isValid;
    
    @property (nonatomic, retain) IBOutlet UIImageView *resultsImage;
    @property (nonatomic, retain) IBOutlet UILabel *resultsLabel;