Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Passing simple data between UIViews

RobbyRobby Posts: 16Registered Users
edited January 2012 in iOS SDK Development
Hi,
This problem has been stumping me for the last day. I have found a soultion, but I don't like it....
I have a Navigation Controller. I want to pass a UIDate from a child UIViewController to a parent UIViewController.
This "should" be easy to do, but I can't seem to figure it out as Xcode won't let me access data from with the parent UIView directly from the child UIView.
The solution I have used is the singleton as detailed here...
MVC on the iPhone: The Model | BIT-101 Blog
But, I don't like it. It seems complicated for the act of simply passing a result from a child selection to a parent.
Does anyone know an easier way to do this?
Thanks!
Rob Beck
New Zealand.
Post edited by Robby on

Replies

  • WaRcLaWzWaRcLaWz Posts: 36Registered Users
    edited May 2009
    Robby wrote: »
    Hi,
    This problem has been stumping me for the last day. I have found a soultion, but I don't like it....
    I have a Navigation Controller. I want to pass a UIDate from a child UIViewController to a parent UIViewController.
    This "should" be easy to do, but I can't seem to figure it out as Xcode won't let me access data from with the parent UIView directly from the child UIView.
    The solution I have used is the singleton as detailed here...
    MVC on the iPhone: The Model | BIT-101 Blog
    But, I don't like it. It seems complicated for the act of simply passing a result from a child selection to a parent.
    Does anyone know an easier way to do this?
    Thanks!
    Rob Beck
    New Zealand.

    I have been stumped with this as well. I cannot figure out why variables and objects have to be so difficult with Objective-C when compared to just about any other language.

    I have fallen back on using external variables instead, which of course is frowned upon for some reason. But until I can figure out how to actually access variables and objects from one class to another, this is working great for me. It should not be this difficult, but it is what it is.
  • RobbyRobby Posts: 16Registered Users
    edited May 2009
    Thanks for the reply!
    WaRcLaWz wrote: »
    I have fallen back on using external variables instead,
    Can you tell me how and where you are defining your external variables? Are they essentially GLOBAL? I could live with that. It's only a small app. Singletons are a pain!
    WaRcLaWz wrote: »
    It should not be this difficult, but it is what it is.
    Agreed.... I previously used Microsoft Visual Studio for my OOP programming (C# mainly), and it is much easier to do here, but then the VS environment doesn't try to be MVC compliant.

    Regards
    Rob.
  • deansxdeansx Posts: 127Registered Users
    edited May 2009
    Robby wrote: »
    Hi,
    This problem has been stumping me for the last day. I have found a soultion, but I don't like it....
    I have a Navigation Controller. I want to pass a UIDate from a child UIViewController to a parent UIViewController.
    This "should" be easy to do, but I can't seem to figure it out as Xcode won't let me access data from with the parent UIView directly from the child UIView.
    The solution I have used is the singleton as detailed here...
    MVC on the iPhone: The Model | BIT-101 Blog
    But, I don't like it. It seems complicated for the act of simply passing a result from a child selection to a parent.
    Does anyone know an easier way to do this?
    Thanks!
    Rob Beck
    New Zealand.

    Hi Rob-

    I strongly urge you to consider the shared instance approach described here:

    The Death of Global Variables @ Dr. Touch

    Global variables seem to always have a way of coming back to bite me as a project evolves. Mr. Drobnik generally seems to give very good, easy to follow, advice, and this is certainly no exception.
    <a href="http://www.blumtnwerx.com"; target="_blank"><font face="System"><b><font color="Blue">BluMtnWerx</font></b></font><br />
    </a><br />
    <i>Developers of:</i><blockquote><a href="http://www.blumtnwerx.com/TiltBall.html"; target="_blank">TiltBall and T
  • WaRcLaWzWaRcLaWz Posts: 36Registered Users
    edited May 2009
    Sure.

    I created a blank foundation class called "Globals" that stores the variables.

    Globals.h looks like this...
    #import <Foundation/Foundation.h>
    
    extern NSArray *gMapArray;
    extern NSMutableArray *gPathHexArray;
    extern NSMutableArray *gTerrainImageViewArray;
    
    
    @interface Globals : NSObject {
    
    }
    
    @end
    

    Globals.m looks like this...
    #import "Globals.h"
    
    @implementation Globals
    
    NSArray *gMapArray;
    NSMutableArray *gPathHexArray;
    NSMutableArray *gTerrainImageViewArray;
    
    
    @end
    

    The external variables need to be initialized before they can be used, so I did this in the "AppDelegate.m" class...
    - (void)applicationDidFinishLaunching:(UIApplication *)application {    
        
        // Override point for customization after app launch
        [window addSubview:mainMenuViewController.view];
        [window makeKeyAndVisible];
    
    	//Initialize array that holds the terrain graphics
    	gMapArray = [[NSArray alloc] init];
    	//Initialize array that holds the movement hexes
    	gPathHexArray = [[NSMutableArray alloc] init];
    	//Initialize array that holds the terrain UIImageView objects
    	gTerrainImageViewArray = [[NSMutableArray alloc] init];
    }
    

    You must make sure that you import the "Globals.h" into any class that uses the external variables, including the AppDelegate that I mentioned above. For example, in the "AppDelegate.m" you would make sure to have
    #import "Globals.h"
    


    I hope this helps! :)
  • RobbyRobby Posts: 16Registered Users
    edited May 2009
    Thanks guys,

    Dean, I will definitely take a look at using shared instances. It looks like a very tidy way to do it.
    WaRcLaWz, this looks logical, and could be an option for my app.

    Globals have been part of my programming life for almost 20 years now... I try not to use them, but sometimes it is just easier for those small single instance, single developer apps.

    Coming from an assembly background on 2k embedded processors, all this OOP for embedded systems is quite new for me. :)

    Cheers
    Rob.
  • WaRcLaWzWaRcLaWz Posts: 36Registered Users
    edited May 2009
    I too have used global variables for a very long time, and I honestly have never had any issues with them. I constantly see people stating that they should not be used, but I have never seen any good reasons why not.

    For example in the article, the example of Shared Instances does not seem like it would be easier to follow than using the Globals.h/Globals.m classes that I use. All a programmer would need to do is look into the Globals class and see them all in one place. Not too difficult for me anyway.

    One thing I forgot to mention about global variables though is that you should give them names that identify them as such. I always prefix a global variable with a lowercase "g". This way, not matter where you are in your code, you will know which variables are globals. And if you wish to find where they are declared you know exactly where to look... Globals...
  • alain55alain55 Posts: 3New Users
    edited June 2009
    I use global var to pass data between two views but this seems not to work between two non-adjacent views. Let me explain.

    1. I write the glob var in view 1.
    2. I push down in view 2, I can read the global var ok.
    3. I push down in view 3, the global var are not there anymore.

    I have also tried to copy the glob vars in view 2 to new ones, and these are
    not readable in view 3

    Any ideas why this behavior? and are glob var really global throughout the whole app or just on one view controller.
  • RobbyRobby Posts: 16Registered Users
    edited June 2009
    alain55 wrote: »
    I use global var to pass data between two views but this seems not to work between two non-adjacent views. Let me explain.

    1. I write the glob var in view 1.
    2. I push down in view 2, I can read the global var ok.
    3. I push down in view 3, the global var are not there anymore.

    I have also tried to copy the glob vars in view 2 to new ones, and these are
    not readable in view 3

    Any ideas why this behavior? and are glob var really global throughout the whole app or just on one view controller.

    Have you included the header for view 1 in view 4?
    I have seen on occasions global variable losing scope, but it's normally due to the fact that I haven't reference them correctly.
    Globals can be dangerous in this respect.
    Hope this helps.
  • alain55alain55 Posts: 3New Users
    edited June 2009
    Robby wrote: »
    Have you included the header for view 1 in view 4?
    I have seen on occasions global variable losing scope, but it's normally due to the fact that I haven't reference them correctly.
    Globals can be dangerous in this respect.
    Hope this helps.

    Yes I have. As a test, i imported all of the headers.

    The error I get is:
    Warning: Unable to read symbols from "UIKit" (not yet mapped into memory).
    warning: Unable to read symbols for "/System/Library/Frameworks/CoreGraphics.framework/CoreGraphics" (file not found).
    warning: Unable to read symbols from "CoreGraphics" (not yet mapped into memory).


    The NSLog shows:
    gpage.myconc string = <UIImageView: 0x554950>
    gConc = <UIImageView: 0x554950>
  • WaRcLaWzWaRcLaWz Posts: 36Registered Users
    edited June 2009
    alain55 wrote: »
    Yes I have. As a test, i imported all of the headers.

    The error I get is:
    Warning: Unable to read symbols from "UIKit" (not yet mapped into memory).
    warning: Unable to read symbols for "/System/Library/Frameworks/CoreGraphics.framework/CoreGraphics" (file not found).
    warning: Unable to read symbols from "CoreGraphics" (not yet mapped into memory).


    The NSLog shows:
    gpage.myconc string = <UIImageView: 0x554950>
    gConc = <UIImageView: 0x554950>


    I believe that error message is one that you will always get when the application crashes.

    I am confused about your code "gpage.myconc string" since the log shows it as a UIImageView yet it should be a string. I am thinking that maybe your code is expecting a string but is finding a UIImageView instead.
  • irene_19891023irene_19891023 Posts: 7New Users
    edited May 2010
    WaRcLaWz wrote: »
    Sure.

    I created a blank foundation class called "Globals" that stores the variables.

    Globals.h looks like this...
    #import <Foundation/Foundation.h>
    
    extern NSArray *gMapArray;
    extern NSMutableArray *gPathHexArray;
    extern NSMutableArray *gTerrainImageViewArray;
    
    
    @interface Globals : NSObject {
    
    }
    
    @end
    

    Globals.m looks like this...
    #import "Globals.h"
    
    @implementation Globals
    
    NSArray *gMapArray;
    NSMutableArray *gPathHexArray;
    NSMutableArray *gTerrainImageViewArray;
    
    
    @end
    

    The external variables need to be initialized before they can be used, so I did this in the "AppDelegate.m" class...
    - (void)applicationDidFinishLaunching:(UIApplication *)application {    
        
        // Override point for customization after app launch
        [window addSubview:mainMenuViewController.view];
        [window makeKeyAndVisible];
    
    	//Initialize array that holds the terrain graphics
    	gMapArray = [[NSArray alloc] init];
    	//Initialize array that holds the movement hexes
    	gPathHexArray = [[NSMutableArray alloc] init];
    	//Initialize array that holds the terrain UIImageView objects
    	gTerrainImageViewArray = [[NSMutableArray alloc] init];
    }
    

    You must make sure that you import the "Globals.h" into any class that uses the external variables, including the AppDelegate that I mentioned above. For example, in the "AppDelegate.m" you would make sure to have
    #import "Globals.h"
    


    I hope this helps! :)


    Hello~I have known your method, but I still have a question that do i need to write "Global *global=[[Global alloc] init];" before i use those global variables? Thank you~!
  • roelienroelien Posts: 1New Users
    edited January 2012
    WaRcLaWz wrote: »
    I hope this helps! :)

    After all these years, you still made someone happy!
    :)
Sign In or Register to comment.