Amazon.com Widgets if statement doesn't work as it should!!!
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 24, 2013, 05:15:31 PM
Home Help Search chat Login Register   
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+  Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+  iOS Programming
| |-+  General Questions
| | |-+  if statement doesn't work as it should!!!
Pages: [1]   Go Down
Print
Author Topic: if statement doesn't work as it should!!!  (Read 567 times)
JAlizadeh
Newbie
*
Posts: 7


Email




« on: August 10, 2011, 05:28:12 PM »

Does anyone understand what is wrong here?  For UIButton Blue Background, if statement doesn't work.  For UIButton Green Background, it works fine.  Everything else works properly.  Thanks,

#define normalButton [UIImage imageNamed:@"blueCategoryBackgroundImage.png"]
#define selectedButton [UIImage imageNamed:@"greenCategoryBackgroundImage.png"]

//When General Button is clicked, UIButton Background changes to Blue Background or Green Background based on previous selection
-(IBAction) generalButtonClicked
{   
    //Setup logic for Background Image
    if (generalButton.currentBackgroundImage != nil)
    {
        if ([generalButton.currentBackgroundImage isEqual: normalButton])
       {
            [generalButton setBackgroundImage: selectedButton forState:UIControlStateNormal];
        }
        else
        {
            [generalButton setBackgroundImage: normalButton forState:UIControlStateNormal];
        }
       
    }
    else
    {
        NSLog(@"There is a problem with current Background Image.");
    }
   
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
       
    //remove existing saved image
    if ([userDefaults dataForKey:@"imageGeneral"] != nil)
    {
        [userDefaults removeObjectForKey: @"imageGeneral"];
    }
   
    //Save New Background Image
    NSData *imageGeneralData = UIImagePNGRepresentation(generalButton.currentBackgroundImage);
    [userDefaults setObject:imageGeneralData forKey:@"imageGeneral"];
    [userDefaults synchronize];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
     
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
   
//retrieve imageGeneral
    if ([userDefaults dataForKey:@"imageGeneral"] != nil)
    {
        NSData *imgGeneralData = [userDefaults dataForKey:@"imageGeneral"];
        UIImage *imgGeneral = [UIImage imageWithData:imgGeneralData];
        [generalButton setBackgroundImage:imgGeneral forState:UIControlStateNormal];
    }
    else
    {
        NSLog(@"There is a problem with imageGeneral Retrieval.");
    }

    [userDefaults synchronize];
}
Logged
skochan
Administrator
Hero Member
*****
Posts: 3106







« Reply #1 on: August 10, 2011, 07:41:07 PM »

Each time you execute the imageNamed: method through either normalButton or selectedButton you are creating a new instance from the UIImage class, and thus a different reference to a different object.  They'll never match.  Once you fix that, you can just use the normal == operator to test if the two objects are identical.

Cheers,

Steve
Logged
JAlizadeh
Newbie
*
Posts: 7


Email




« Reply #2 on: August 11, 2011, 03:37:38 PM »

Thanks Steve,  I think you are saying to not create new object with normalButton below.

        if ([generalButton.currentBackgroundImage isEqual: normalButton])

I created UIButton with IB.  Also, I set the Background image with that too.  I need to figure out how to using existing object based on that setup.  I'll continue to study it.

Thanks,
Logged
skochan
Administrator
Hero Member
*****
Posts: 3106







« Reply #3 on: August 11, 2011, 04:15:36 PM »

You can also keep an instance variable that tracks the image on the button (or use the button's tag property if you want).

Cheers,

Steve
Logged
JAlizadeh
Newbie
*
Posts: 7


Email




« Reply #4 on: August 14, 2011, 10:02:08 PM »

Using Button's Tag seems to be the easiest way to go.  Thanks for your guidance.
Logged
Pages: [1]   Go Up
Print
Jump to:  



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.