Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

AppFusion - 6 in 1!
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 05-29-2009, 06:44 PM   #1 (permalink)
Registered Member
 
Join Date: May 2009
Location: Auckland, New Zealand
Posts: 16
Send a message via Skype™ to Robby
Default Passing simple data between UIViews

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.
Robby is offline   Reply With Quote
Old 05-29-2009, 07:41 PM   #2 (permalink)
Registered Member
 
Join Date: May 2009
Posts: 36
Default

Quote:
Originally Posted by Robby View Post
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.
WaRcLaWz is offline   Reply With Quote
Old 05-29-2009, 07:49 PM   #3 (permalink)
Registered Member
 
Join Date: May 2009
Location: Auckland, New Zealand
Posts: 16
Send a message via Skype™ to Robby
Default

Thanks for the reply!

Quote:
Originally Posted by WaRcLaWz View Post
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!
Quote:
Originally Posted by WaRcLaWz View Post
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.
Robby is offline   Reply With Quote
Old 05-29-2009, 07:57 PM   #4 (permalink)
Objective-C Code Monkey
 
deansx's Avatar
 
Join Date: May 2009
Location: Mountain View, CA
Posts: 127
Default

Quote:
Originally Posted by Robby View Post
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.
deansx is offline   Reply With Quote
Old 05-29-2009, 08:04 PM   #5 (permalink)
Registered Member
 
Join Date: May 2009
Posts: 36
Default

Sure.

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

Globals.h looks like this...
Code:
#import <Foundation/Foundation.h>

extern NSArray *gMapArray;
extern NSMutableArray *gPathHexArray;
extern NSMutableArray *gTerrainImageViewArray;


@interface Globals : NSObject {

}

@end
Globals.m looks like this...
Code:
#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...

Code:
- (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
Code:
#import "Globals.h"

I hope this helps!
WaRcLaWz is offline   Reply With Quote
Old 05-29-2009, 08:15 PM   #6 (permalink)
Registered Member
 
Join Date: May 2009
Location: Auckland, New Zealand
Posts: 16
Send a message via Skype™ to Robby
Default

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.
Robby is offline   Reply With Quote
Old 05-29-2009, 08:25 PM   #7 (permalink)
Registered Member
 
Join Date: May 2009
Posts: 36
Default

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

Last edited by WaRcLaWz; 05-29-2009 at 08:28 PM.
WaRcLaWz is offline   Reply With Quote
Old 06-11-2009, 06:45 PM   #8 (permalink)
New Member
 
Join Date: Jun 2009
Posts: 3
Default

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.
alain55 is offline   Reply With Quote
Old 06-11-2009, 08:05 PM   #9 (permalink)
Registered Member
 
Join Date: May 2009
Location: Auckland, New Zealand
Posts: 16
Send a message via Skype™ to Robby
Default

Quote:
Originally Posted by alain55 View Post
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.
Robby is offline   Reply With Quote
Old 06-12-2009, 12:33 PM   #10 (permalink)
New Member
 
Join Date: Jun 2009
Posts: 3
Default

Quote:
Originally Posted by Robby View Post
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>
alain55 is offline   Reply With Quote
Old 06-18-2009, 02:13 AM   #11 (permalink)
Registered Member
 
Join Date: May 2009
Posts: 36
Default

Quote:
Originally Posted by alain55 View Post
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.
WaRcLaWz is offline   Reply With Quote
Old 05-04-2010, 12:57 AM   #12 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 7
Default still about variable shared between UIViews

Quote:
Originally Posted by WaRcLaWz View Post
Sure.

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

Globals.h looks like this...
Code:
#import <Foundation/Foundation.h>

extern NSArray *gMapArray;
extern NSMutableArray *gPathHexArray;
extern NSMutableArray *gTerrainImageViewArray;


@interface Globals : NSObject {

}

@end
Globals.m looks like this...
Code:
#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...

Code:
- (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
Code:
#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~!
irene_19891023 is offline   Reply With Quote
Old 01-15-2012, 07:35 AM   #13 (permalink)
Registered Member
 
Join Date: Jan 2012
Location: Netherlands
Posts: 1
Thumbs up wow

Quote:
Originally Posted by WaRcLaWz View Post
I hope this helps!
After all these years, you still made someone happy!
roelien is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 633
21 members and 612 guests
ADY, bandley, BrianSlick, Creativ, dacapo, Dattee, djbrooks111, gbenna, HDshot, IphoneSdk, iseff, jakerocheleau, jbro, joeallenpro, kampftrinker, linkmx, mer10, nimesh_158, Reyna, yurikus
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 160,619
Threads: 89,833
Posts: 383,490
Top Poster: BrianSlick (7,244)
Welcome to our newest member, yurikus
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 04:43 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0