using uiimage in an if (condition) statement

Discussion in 'iOS Programming' started by gbenna, Feb 23, 2012.

  1. gbenna, Feb 23, 2012
    Last edited by a moderator: Feb 23, 2012

    macrumors member

    Joined:
    Jul 27, 2011
    #1
    I want to display a navigationBar title from a NSDictionary. In the firstDetailViewController I have a UIImageView which changes depending on the row selected in the pushing tableview controller.

    I want to use these changing images to determining what the navigationBar title (among other things) will be in a secondDetailViewController which is pushed by the firstDetailViewController.

    Here is my code for the firstDetailViewController;


    Code:
    -(IBAction)pushView:(id) sender{
        
        secondDetailViewController *secondDetail = [[secondDetailViewController alloc] init]; 
        
        
        if ((self.imageView.image = [UIImage imageNamed:@"0002.jpg"])) {
            NSMutableDictionary *myDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                                  @"TitleA", @"headerkey2", nil];
                                              
           secondDetail.myDictionary = myDictionary;
           
            NSLog(@"%@", [mapDictionary objectForKey:@"headerkey2"]);
        }
        
        else if((self.imageView.image = [UIImage imageNamed:@"0003.jpg"])) {
            NSMutableDictionary *myDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                                  @"TitleB", @"headerkey2",nil];
                                                  
            secondDetail.myDictionary = myDictionary;
            
           NSLog(@"%@", [mapDictionary objectForKey:@"headerkey2"]);
        }
    
        [self.navigationController pushViewController:secondDetail animated:YES];    
                                                           
                                                    }
    in my secondDetailViewController's viewDidLoad my code is;

    Code:
    self.title = [(NSMutableDictionary *)myDictionary objectForKey:@"headerkey2"];
    This is not working, only the TitleA shows up no matter what.

    Can I use a UIImage as a condition and if so what am I doing wrong.

    Thanks for the help.
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    1) Is = a comparison or assignment operator

    2) Can you compare UIImage instances in this way? What makes you sure that a UIImage returned by imageNamed: is the same instance as one already in use? It might be due to caching. But perhaps it might not be (although the above is a more pressing error)
     
  3. thread starter macrumors member

    Joined:
    Jul 27, 2011
    #3
    using uiimage in a if (condition) statement

    You are right. I changed it to an == statement and now everything is working fine. Thanks.
     
  4. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    Alright, now that you've addressed issue #1, you should make sure you've addressed issue #2. Just because your code works fine now, doesn't necessarily mean it always will.
     
  5. thread starter macrumors member

    Joined:
    Jul 27, 2011
    #5
    Using uiimage in a if (condition) statement

    I have written several else if statements on this and it works fine. Thanks for all the help.
     
  6. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    for now, perhaps...
     

Share This Page