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,
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.
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
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.
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:
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.
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
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.
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...
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.
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.
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).
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).
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.
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~!