PDA

View Full Version : using uiimage in an if (condition) statement




gbenna
Feb 23, 2012, 10:25 AM
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.



robbieduncan
Feb 23, 2012, 10:52 AM
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)

gbenna
Feb 23, 2012, 12:16 PM
You are right. I changed it to an == statement and now everything is working fine. Thanks.

dejo
Feb 23, 2012, 02:02 PM
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)

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

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.

gbenna
Feb 23, 2012, 03:43 PM
I have written several else if statements on this and it works fine. Thanks for all the help.

dejo
Feb 23, 2012, 03:51 PM
I have written several else if statements on this and it works fine...

for now, perhaps...