addressalign-toparrow-leftarrow-rightbackbellblockcalendarcameraccwcheckchevron-downchevron-leftchevron-rightchevron-small-downchevron-small-leftchevron-small-rightchevron-small-upchevron-upcircle-with-checkcircle-with-crosscircle-with-pluscrossdots-three-verticaleditemptyheartexporteye-with-lineeyefacebookfolderfullheartglobegmailgooglegroupsimageimagesinstagramlinklocation-pinm-swarmSearchmailmessagesminusmoremuplabelShape 3 + Rectangle 1outlookpersonJoin Group on CardStartprice-ribbonImported LayersImported LayersImported Layersshieldstartickettrashtriangle-downtriangle-uptwitteruseryahoo

Orlando iOS Developer Group Message Board › Pass Variables between ViewContorllers easier than passing a Kidney Stone Us

Pass Variables between ViewContorllers easier than passing a Kidney Stone Using the Singleton Design Method?

Shane H.
user 14514466
Orlando, FL
Post #: 13
Here is a video
http://youtu.be/XjU4k...­
that shows how to use the prepareforsegue method. Part 1 and Part 2. But be sure to read on and learn about using the singleton Method.

I am trying to pass one variable from ViewController A to display in ViewController B using the Singleton Design Method. They say its easy to pass an object like this, but it feels like passing a Kidney stone.

Apparently this is a very powerful way to gain access to variables and objects in your application. It seems easy enough to set up, but I can not figure out how to implement it. Any advise would be much appreciated.

Here is a straight forward presentation, that explains it in an easy way to follow.

Here is a great singleton tutorial.

Here is the setup from Singleton Method.

Here is a video tutorial.

THIS IS THE PART THAT IS DRIVING ME CRAZY....
MyManager *sharedManager = [MyManager sharedManager];

You can implement a singleton class in Objective-C using the following code:

MyManager.h


#import <foundation/Foundation.h>

@interface MyManager : NSObject {
NSString *someProperty;
}

@property (nonatomic, retain) NSString *someProperty;

+ (id)sharedManager;

@end

MyManager.m




#import "MyManager.h"

static MyManager *sharedMyManager = nil;

@implementation MyManager

@synthesize someProperty;

#pragma mark Singleton Methods
+ (id)sharedManager {
@synchronized(self) {
if (sharedMyManager == nil)
sharedMyManager = [[self alloc] init];
}
return sharedMyManager;
}
- (id)init {
if (self = [super init]) {
someProperty = [[NSString alloc] initWithString:@"Default Property Value"];
}
return self;
}
- (void)dealloc {
// Should never be called, but just here for clarity really.
}

@end

If you are not using Automatic Reference Counting (ARC), then you should use the following code:

MyManager.m




#import "MyManager.h"

static MyManager *sharedMyManager = nil;

@implementation MyManager

@synthesize someProperty;

#pragma mark Singleton Methods
+ (id)sharedManager {
@synchronized(self) {
if(sharedMyManager == nil)
sharedMyManager = [[super allocWithZone:NULL] init];
}
return sharedMyManager;
}
+ (id)allocWithZone:(NSZone *)zone {
return [[self sharedManager] retain];
}
- (id)copyWithZone:(NSZone *)zone {
return self;
}
- (id)retain {
return self;
}
- (unsigned)retainCount {
return UINT_MAX; //denotes an object that cannot be released
}
- (oneway void)release {
// never release
}
- (id)autorelease {
return self;
}
- (id)init {
if (self = [super init]) {
someProperty = [[NSString alloc] initWithString:@"Default Property Value"];
}
return self;
}
- (void)dealloc {
// Should never be called, but just here for clarity really.
[someProperty release];
[super dealloc];
}

@end

Then you can reference the singleton from anywhere by calling the following function:
1



MyManager *sharedManager = [MyManager sharedManager];
A former member
Post #: 1
I don't really understand where your problem is. What is (or isn't) happening when you try to implement this?
A former member
Post #: 23

Shane,

Using a singleton to pass a variable is overkill.

Instead, create a public property in the target view controller. Get a pointer to the target view controller and just assign the value to the property of the target view controller.
John D.
user 7287749
Orlando, FL
Post #: 2
NSUserDefaults is an easy way to save integers, strings, and arrays while bouncing through many view controllers. Very little code and they are saved even when you close the app and relaunch:

Set:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:@"my_string" forKey:@"my_string_key"];

Get:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs objectForKey:@"my_string_key"]­


NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; is placed once per method at the top if setObject and objectForKey are used.
Shane H.
user 14514466
Orlando, FL
Post #: 15

Shane,

Using a singleton to pass a variable is overkill.

Instead, create a public property in the target view controller. Get a pointer to the target view controller and just assign the value to the property of the target view controller.


I've gotten both ways to work finally. Now, I'm just wondering when is it appropriate to use either method. Or does it matter?
Shane H.
user 14514466
Orlando, FL
Post #: 16
NSUserDefaults is an easy way to save integers, strings, and arrays while bouncing through many view controllers. Very little code and they are saved even when you close the app and relaunch:

Set:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:@"my_string" forKey:@"my_string_key"];

Get:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs objectForKey:@"my_string_key"]­


NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; is placed once per method at the top if setObject and objectForKey are used.

I like the simplicity of this. I'll give it a shot and let you know at the meetup how I faired.
A former member
Post #: 28
Apple's documentation says, "The NSUserDefaults class provides a programmatic interface for interacting with the defaults system."

In my opinion, using NSUserDefaults as a scratchpad is not good style, because it doing so directly contradicts with the documented use of NSUserDefault. I think using it as a scratch pad will lead to extra-work for the next maintainer of your App.


Rodney


Shane H.
user 14514466
Orlando, FL
Post #: 28
Here is a primer that discusses the two ways in the (my) Universe to pass data between view controllers by making them globally accessible. It appears the Singleton is the preferred way to go:
A former member
Post #: 29
http://en.wikipedia.o...­ explains global variables and why they are generally not good.
Powered by mvnForum

Our Sponsors

People in this
Meetup are also in:

Sign up

Meetup members, Log in

By clicking "Sign up" or "Sign up using Facebook", you confirm that you accept our Terms of Service & Privacy Policy