Are there global variables in xcode?

Discussion in 'iOS Programming' started by tombuarts, Apr 25, 2010.

  1. tombuarts macrumors newbie

    Joined:
    Jan 30, 2010
    #1
    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.
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    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!
     
  3. tombuarts thread starter macrumors newbie

    Joined:
    Jan 30, 2010
    #3
    OK, that sounds easy enough, but can you show me a little snippet of code to accomplish that?
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    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.
     
  5. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #5
    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.
     
  6. tombuarts thread starter macrumors newbie

    Joined:
    Jan 30, 2010
    #6
    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.
     
  7. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    What's with the reference to dataViewController? You sure those shouldn't be mvc?
     
  8. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #8
    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.
     
  9. tombuarts thread starter macrumors newbie

    Joined:
    Jan 30, 2010
    #9
    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.
     

Share This Page