Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

tombuarts

macrumors newbie
Original poster
Jan 30, 2010
15
0
I have an app that has 4 buttons on one view that display a single modal view when pressed. I would like each button to display a different picture in an image view on the modal view. How do I pass the info from the view to the modal view? Does xcode support global variables? I need this one variable to accomplish what I want to do.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
XCode is an IDE. It's not a programming language.

Objective-C is a programming language. And is the one you are using. Whilst it does support global variables I would argue they are almost never the correct solution. In this case they are 100% not the correct solution. You should be able to pass state between your controllers without using global variables. It would seem clear that when your button is pressed you know what the image is and are creating/displaying your modal view right there. Just pass it the image!
 

tombuarts

macrumors newbie
Original poster
Jan 30, 2010
15
0
OK, that sounds easy enough, but can you show me a little snippet of code to accomplish that?
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
Why don't you post your code showing your attempt at doing this. I'm not about to post code for you to copy and paste: you learn nothing that way.
 

firewood

macrumors G3
Jul 29, 2003
8,108
1,345
Silicon Valley
Xcode will compile plain C. And global variables in C are very easy. Just stick them outside any subroutine and reference them with extern.

But if you want a button to tell a view to display something, why not just have the button send the view a message? Even if the button doesn't know which view, it could post a notification for any random view to listen in on.
 

tombuarts

macrumors newbie
Original poster
Jan 30, 2010
15
0
Ok, here's what I have so far.

Code:
DataViewController *mvc = [[[DataViewController alloc] initWithNibName:@"DataView" bundle:[NSBundle mainBundle]] autorelease];
	dataViewController.planet.image = [UIImage imageNamed:@"mercury.png"];
	dataViewController.view.backgroundColor = [UIColor blueColor]; 
	[self presentModalViewController:mvc animated:YES];

The modal view displays, but the image is not "mercury.png". I don't know if this is enough for you to know what is wrong, but let me know. Thanks.
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
It's generally a bad idea to grope other view controllers views. What your code should do when building the view controller to be presented is to have an ivar/property in the second view controller for the image. Set that value, not the imageView like you're doing now. Then in the viewDidLoad method of the second view controller set the image to the imageView. Same for the background color.

A view controller's nib isn't loaded until it's pushed. So the planet imageView doesn't exist at the point that your code is groping it.
 

tombuarts

macrumors newbie
Original poster
Jan 30, 2010
15
0
Thanks to all of you for "pushing" me in the right direction. I got it to work and it looks great. Thanks for the help.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.