using uiimage in an if (condition) statement

Discussion in 'iPhone/iPad Programming' started by gbenna, Feb 23, 2012.

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

    macrumors member

    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;

    -(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;

    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


    Staff Member

    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. macrumors member

    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


    Staff Member

    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. macrumors member

    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


    Staff Member

    for now, perhaps...

Share This Page