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

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

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

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.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 06-17-2010, 09:05 AM   #1 (permalink)
Fly-by-night Innovator
 
Join Date: Jun 2010
Posts: 364
musicwind95 is on a distinguished road
Exclamation Passing data between view controllers

I have set up a nav controller, which works perfectly.

I've seen Lecture 7 of CS193P (Winter 2010), which details nav controllers. It shows how to pass data into a new view controller—I understand that part. I don't understand how to pass data back. The lecture talked about properties, but I'm not very comfortable or familiar with them. When I pass the data back, I want the table view embedded in the previous view (as in, there's a table view in previousViewController.view, but previousViewController itself is *not* a subclass of UITableViewcontroller) to reload itself. I'm not sure how to go about doing this.

In addition, I'd like to do the same thing with modal views, except to pass the data back when the modal view is dismissed.
musicwind95 is offline   Reply With Quote
Old 06-17-2010, 10:52 AM   #2 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,983
smithdale87 is on a distinguished road
Send a message via AIM to smithdale87
Default

Search this forum.
smithdale87 is offline   Reply With Quote
Old 06-17-2010, 12:01 PM   #3 (permalink)
Registered Member
 
Join Date: Jun 2009
Location: Ypsilanti, Michigan
Age: 63
Posts: 1,549
RLScott is on a distinguished road
Default

Quote:
Originally Posted by musicwind95 View Post
I have set up a nav controller, which works perfectly.

I've seen Lecture 7 of CS193P (Winter 2010), which details nav controllers. It shows how to pass data into a new view controller—I understand that part. I don't understand how to pass data back. The lecture talked about properties, but I'm not very comfortable or familiar with them. When I pass the data back, I want the table view embedded in the previous view (as in, there's a table view in previousViewController.view, but previousViewController itself is *not* a subclass of UITableViewcontroller) to reload itself. I'm not sure how to go about doing this.

In addition, I'd like to do the same thing with modal views, except to pass the data back when the modal view is dismissed.
Until you become very familiar with properties, you are not going to be successful in Objective-C programming. Take the time now to learn about them. Brian Slick has a very nice tutorial on them. See his signature on any of his postings. As for reloading a table view, there is a method for that. It is called reloadData. Did you know that in XCODE you can place the cursor over any class name in your code and hold the ALT key and click on it to bring up the complete class reference for that class? It is very good documentation. Anyway, you just need to know when to use the reloadData method. Since the previous view is going to be coming back into view, maybe the viewWillAppear method for that view controller could be the place where you could do it.

Robert Scott
Ypsilanti, Michigan
RLScott is offline   Reply With Quote
Old 06-17-2010, 02:22 PM   #4 (permalink)
Fly-by-night Innovator
 
Join Date: Jun 2010
Posts: 364
musicwind95 is on a distinguished road
Default

Sorry RL...When I said properties I meant protocols. I'm completely comfortable with properties.
musicwind95 is offline   Reply With Quote
Old 06-17-2010, 02:32 PM   #5 (permalink)
Fly-by-night Innovator
 
Join Date: Jun 2010
Posts: 364
musicwind95 is on a distinguished road
Default

Quote:
Originally Posted by smithdale87 View Post
Search this forum.
I just did, but most of the relevant results weren't helpful. I know how to pass data forward, but not back. In addition, I don't know how to get the original view to reload itself based on the data that it receives back.

Basically, parentViewController contains a table view (but it's not a subclass of UITableViewController because I need to display other things). When a row is tapped, it loads detailViewController, which allows the user to change some things. I'm saving all of those changes into a single NSArray. I'd like to be able to pass the NSArray back to parentViewController just as detailViewController is dismissed (when the user taps the back button). I'd then like for parentViewController to reload its table view.

I am also doing this with an Add (+) button in the nav bar, which brings up a modal view controller—like the process of adding songs to the On-The-Go playlist in the Music app.
musicwind95 is offline   Reply With Quote
Old 06-17-2010, 03:16 PM   #6 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 16
justagruvn is on a distinguished road
Default

I would totally like to double High Five this question. Musicwind, I am having the exact same problem. Sofar, this is what I have come up with.. all unsuccessfully.

I am total noob so please take this all with a pleasantly wandering commentary feel.. none should be taken as rule...

As I see it, there are about a billion ways to do this..
1. Create a Singleton:

Plus: It is like a global variable, which is cool in its ease of getting to. You could then make it a pass through that you could get variables from when first level controller loads. Negatives: these oop dudes really dont like them unless you have to use them. Its all fung in the shwei of OOP, etc.etc.

2. Create an instance in your app delegate with pointers. That way The two controllers aren't talking to each other.

3. Another Idea I'm trying.. Try to figure out what the name of the instance of FirstLevelViewController is. that way you could message it directly. I think this would work. Its still out there in memory I think, and it has a name. Its just because rootviewcontroller sets it up automagically at startup I dont know what the instance id is so I can't figure out how to message it.

3. Finally, and I think what Im trying to go with, would be a bit of a purist OOP in my estimation.. Which would be creating a model class with one instance of said class that would hold the data. You could then have the view controllers reference instanced model class on viewdidappears. I think this is good because 1. It is keeping data out of the controller (which lets face it this data really is data not related to the view controller other than for its viewing purposes). So, that information out of the view controller and into a happy little data class of its own. This way you could name the instance on your own at some point, you would know its name for messaging purposes.

Other options I've seen thrown about involve using NSNotification center, but I think these people were diseased or high at the time. Crazy talk.

Please let me know if you figure this out. Others, feel free to smash/bash/trash all of this commentary. I'm really excited about figuring this out.

Peace, love, recycle.
justagruvn is offline   Reply With Quote
Old 06-17-2010, 04:00 PM   #7 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,983
smithdale87 is on a distinguished road
Send a message via AIM to smithdale87
Default

Quote:
Other options I've seen thrown about involve using NSNotification center, but I think these people were diseased or high at the time. Crazy talk.
Agreed.

Using notifications is more of a "bolt-on" approach. It typically indicates some design flaws when used for this purpose.
smithdale87 is offline   Reply With Quote
Old 06-17-2010, 04:13 PM   #8 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 16
justagruvn is on a distinguished road
Default

O.k. so I tried one of those ramblings and would like some help.

I made a Model named "Model.h"

@interface Model : NSObject {

NSString *date;
}

@property (nonatomic, retain) NSString *date;


@end


I then attempted to implement said Model - named "Model.m" (I know this is probably off)


#import "Model.h"

@implementation Model
@synthesize date;


@end

In my firstLevelViewController, on viewdidload, I believe I created an instance of this object.. confusingly named "model" just to screw myself.


Model *model = [[Model alloc]init];
model.date = @"Oh No You Didn't";
self.title = [model date]; <-- just to try and figure out what is stored in there.


Now, when they go to one of my second level view controllers. They use a datepicker to pick a date, I figure out how old they are in months, make a string saying "you're this old".. store it into a *message.

What I want to do now, I think, is to send a message to the instance of my data model, which is of class Model and instance model and update the date string to the message string... Any ideas how to do this? when I do this:


[Model model.date = message];

Warning: expected ']' before '.' token.
and
"Model may not respond to '+model'.

Any ideas how to clean this up? I would be forever indebted.
justagruvn is offline   Reply With Quote
Old 06-17-2010, 04:16 PM   #9 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,983
smithdale87 is on a distinguished road
Send a message via AIM to smithdale87
Default

If you had an pointer to your model in that 2nd class, you would just need to do
Code:
 model.date = message;
smithdale87 is offline   Reply With Quote
Old 06-17-2010, 04:21 PM   #10 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 16
justagruvn is on a distinguished road
Default

Quote:
Originally Posted by smithdale87 View Post
If you had an pointer to your model in that 2nd class, you would just need to do
Code:
 model.date = message;

when I do that:

model.date = message;

it returns

error: 'model' undeclared (first use in this function).

I think you're spot on with this "pointer" business.. I've heard people talking about them. They sound important. Any ideas how to add some pointing action to that Model class model.date variable?
justagruvn is offline   Reply With Quote
Old 06-17-2010, 04:25 PM   #11 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,983
smithdale87 is on a distinguished road
Send a message via AIM to smithdale87
Default

Quote:
when I do that:

model.date = message;

it returns

error: 'model' undeclared (first use in this function).
Hence my comment "If you had an pointer to your model in that 2nd class"


Since you want this to be sort of a global data management class, you really dont need to worry about the "pointer" business. However, it seems like you're lacking some fairly basic programming knowledge that has nothing to do with obj-c/iphone programming.

I'd google for some tips on how to make your Model class into a singleton.
smithdale87 is offline   Reply With Quote
Old 06-17-2010, 04:46 PM   #12 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 16
justagruvn is on a distinguished road
Default

Quote:
Originally Posted by smithdale87 View Post

"some fairly basic programming knowledge that has nothing to do with obj-c/iphone programming."
Probably a good *understatement. Get it? it was a pointer joke.

Though I have been toiling with this singleton idea.. I would really prefer struggling through this one instance of a data class idea, and learning how to make a pointer to it, as this is a mondo critical step in having classes talk together. I'm trying really hard to be as pure of an oop person from the get-go as it seems like a fun thing to laugh about at cocktail parties.

Most times when I read about pointers, they start getting into the "its a location, like a phone number" to a data location. If I'm not mistaken I did make a pointer to model when I created the instance of it in the firstviewcontroller. So, I think I just need to point my second class to that pointer?

Any help? speaking slowly in small words would be appreciated.
justagruvn is offline   Reply With Quote
Old 06-17-2010, 04:49 PM   #13 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,983
smithdale87 is on a distinguished road
Send a message via AIM to smithdale87
Default

Correct, you need to give your second class that same pointer!
smithdale87 is offline   Reply With Quote
Old 06-17-2010, 04:56 PM   #14 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 16
justagruvn is on a distinguished road
Default

Quote:
Originally Posted by smithdale87 View Post
Correct, you need to give your second class that same pointer!
How do I do that? Can't find an article out there that tells me how.
justagruvn is offline   Reply With Quote
Old 06-17-2010, 04:57 PM   #15 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,983
smithdale87 is on a distinguished road
Send a message via AIM to smithdale87
Default

That depends totally on the structure of your application. Post some code. Just enough to show what you are attempting with class 1 and class 2.
smithdale87 is offline   Reply With Quote
Old 06-17-2010, 05:05 PM   #16 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 16
justagruvn is on a distinguished road
Default

Quote:
Originally Posted by smithdale87 View Post
That depends totally on the structure of your application. Post some code. Just enough to show what you are attempting with class 1 and class 2.
I think I have 3 classes: view controllers named FirstLevelViewController, A secondlevelviewcontroller called "Birthday", and a Data Object NS object named Model.

I would like instances of firstlevelviewcontroller and birthday to be able to get and send messages from model.

Up at the top I posted my model's .h and .m.

FirstlevelViewController actually creates the Model instance "model"


-(void)viewDidLoad {


NSMutableArray *array = [[NSMutableArray alloc]init];

BirthDay *birthday = [[BirthDay alloc]initWithNibName:@"Birthday" bundle:nil];
birthday.title = @"Birth Date";
[array addObject:birthday];
[birthday release];

Height *height = [[Height alloc]initWithNibName:@"Height" bundle:nil];
height.title = @"Height";
[array addObject:height];
[height release];

self.controllers = array;


Model *model = [[Model alloc]init];
model.date = @"Oh No You Didn't";
self.title = [model date];



Birthday.m -- the secondviewcontroller -- I think is the one that needs a pointer to instance of model created by firstviewcontroller.

its code is such which has an action that I want to use to among other things update the model

-(IBAction)updateLabel {

NSDate *dateOfBirth = [datePicker date];
NSDate *todaysDate = [NSDate date];

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSUInteger unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit;

NSDateComponents *components = [gregorian components:unitFlags fromDate:dateOfBirth toDate:todaysDate options:0];
NSInteger months = [components month];
NSInteger days = [components day];

float Months = months;
if (days > 14) {
Months = Months + 0.5;
}





NSString *message = [[NSString alloc]initWithFormat: @"%.1f Months and %d Days old", Months, days];

dateLabel.text = message;

model.date = message;

}

Sos, I think I need to have birthday.m point to the instance of model.

Peace, love, applesauce
justagruvn is offline   Reply With Quote
Old 06-17-2010, 07:13 PM   #17 (permalink)
Fly-by-night Innovator
 
Join Date: Jun 2010
Posts: 364
musicwind95 is on a distinguished road
Default

Isn't there a specific formatting option for code? I'm used to seeing code the way Xcode displays it; my eyes glaze over the code when it's just typed out.

Anyway, I've been playing around with a new project, just to test this. In the RootViewController, I've defined a label and a button. In the SecondViewController, I've defined two buttons and a label. Here's the code: *

There seems to be an issue with the protocol, but as I said before, I'm not very good with protocols.

The interface is set up properly, and everything's connected. I've narrowed down the issue to the protocol. The app works perfectly, except that it crashes when I click the back button from SecondViewController.

*Code is below due to a forum limit on images...?
musicwind95 is offline   Reply With Quote
Old 06-17-2010, 07:14 PM   #18 (permalink)
Fly-by-night Innovator
 
Join Date: Jun 2010
Posts: 364
musicwind95 is on a distinguished road
Default

RootViewController.h
#import <UIKit/UIKit.h>
#import "SecondViewController.h"

@interface RootViewController : UIViewController <passDataBack> {
UILabel *display;
NSString *displayText;
}
@property (nonatomic, retain) IBOutlet UILabel *display;
@property (nonatomic, retain) NSString *displayText;

- (IBAction)pushViewController;
@end


RootViewController.m
#import "RootViewController.h"
@implementation RootViewController
@synthesize display, displayText;

- (IBAction)pushViewController {
SecondViewController *vc = [[SecondViewController alloc] init];
[self.navigationController pushViewController:vc animated:YES];
}

- (void)viewDidLoad {
self.title = @"Root View";
[super viewDidLoad];

// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)viewWillAppearBOOL)animated {
if (self.displayText != nil)
self.display.text = displayText;
[super viewWillAppear:animated];
}

- (void)buttonPressedNSString *)buttonTitle {
self.displayText = buttonTitle;
}

- (void)viewDidUnload {
// Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
// For example: self.myOutlet = nil;
self.display = nil;
self.displayText = nil;
}


- (void)dealloc {
[display release];
[displayText release];
[super dealloc];
}
@end


Rest of the code below:
musicwind95 is offline   Reply With Quote
Old 06-17-2010, 07:15 PM   #19 (permalink)
Fly-by-night Innovator
 
Join Date: Jun 2010
Posts: 364
musicwind95 is on a distinguished road
Default

SecondViewController.h
#import <UIKit/UIKit.h>
@interface SecondViewController : UIViewController {
UIButton *one;
UIButton *two;
UILabel *display;

NSString *buttonPressed;
}
@property (nonatomic, retain) IBOutlet UIButton *one;
@property (nonatomic, retain) IBOutlet UIButton *two;
@property (nonatomic, retain) IBOutlet UILabel *display;

@property (nonatomic, retain) NSString *buttonPressed;

- (IBAction)buttonWasPressedid)sender;

@end

@protocol passDataBack<NSObject>
- (void)buttonPressedNSString *)buttonTitle;
@end


SecondViewController.m
#import "SecondViewController.h"
@implementation SecondViewController
@synthesize one, two, buttonPressed, display;

- (IBAction)buttonWasPressedid)sender {
if (sender == one) {
display.text = @"Button one pressed.";
self.buttonPressed = @"One";
}
else {
display.text = @"Button two pressed.";
self.buttonPressed = @"Two";
}
}
- (void)viewDidLoad {
self.title = @"Detail View";
[super viewDidLoad];
}

- (void)viewWillDisappearBOOL)animated {
id<passDataBack> delegate;
[delegate buttonPressed:buttonPressed];
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
self.one = nil;
self.two = nil;
self.display = nil;
self.buttonPressed = nil;
}


- (void)dealloc {
[one release];
[two release];
[display release];
[buttonPressed release];
[super dealloc];
}
@end


BTW the purple faces are the system processing the colon and parenthesis due to an argument in the method. Sorry about that.

I'd appreciate any suggestions. Just probably would too.
musicwind95 is offline   Reply With Quote
Old 06-18-2010, 04:14 PM   #20 (permalink)
Fly-by-night Innovator
 
Join Date: Jun 2010
Posts: 364
musicwind95 is on a distinguished road
Default

Any ideas?
musicwind95 is offline   Reply With Quote
Old 06-18-2010, 09:12 PM   #21 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 16
justagruvn is on a distinguished road
Default

Crickets?

I think it looks fantastic! really good stuff. I have no idea what a protocol is or does, but boy it sure does look cool.

Again, I will ask... How to I set a pointer from one class object to a different class object? Its really making me bonkers. I feel like Im taking crazy pills here.

Tag.
justagruvn is offline   Reply With Quote
Old 06-19-2010, 10:01 AM   #22 (permalink)
Fly-by-night Innovator
 
Join Date: Jun 2010
Posts: 364
musicwind95 is on a distinguished road
Default

Quote:
Originally Posted by justagruvn View Post
Crickets?
Yep. We've been abandoned.

Quote:
Originally Posted by justagruvn View Post
I think it looks fantastic! really good stuff. I have no idea what a protocol is or does, but boy it sure does look cool.
It is cool...in theory. In reality, I can't for the life of me get it to work.

Quote:
Originally Posted by justagruvn View Post
Again, I will ask... How to I set a pointer from one class object to a different class object? Its really making me bonkers. I feel like Im taking crazy pills here.
Conceptually, I'm not sure, but I think you just need to create an instance of Model in the root's header file, like

@class Model
// ...Some code
Model *model

and then in the implementation of the root, have something like:

#import "RootViewController.h"
#import "Model.h"
- (void)viewDidLoad {
model = [[Model alloc] init];
// ...etc.
}


Make sure to release it in -dealloc. I think that's what you're going for, based on the error message you posted above. Hope this helps!
musicwind95 is offline   Reply With Quote
Old 06-24-2010, 05:25 PM   #23 (permalink)
Fly-by-night Innovator
 
Join Date: Jun 2010
Posts: 364
musicwind95 is on a distinguished road
Unhappy Why isn't this working?

I've modified the code above, but it still isn't working. Any ideas?

Code:
// Nav_Practice App Delegate.h
#import <UIKit/UIKit.h>
 
@interface Nav_PracticeAppDelegate : NSObject <UIApplicationDelegate> {
    
    UIWindow *window;
    UINavigationController *navigationController;
}
 
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
 
@end
Code:
// App Delegate.m
 
#import "Nav_PracticeAppDelegate.h"
 
@implementation Nav_PracticeAppDelegate
 
@synthesize window;
@synthesize navigationController;
 
 
#pragma mark -
#pragma mark Application lifecycle
 
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    // Override point for customization after app launch    
	
	[window addSubview:[navigationController view]];
    [window makeKeyAndVisible];
	return YES;
}
 
 
- (void)applicationWillTerminate:(UIApplication *)application {
	// Save data if appropriate
}
 
 
#pragma mark -
#pragma mark Memory management
 
- (void)dealloc {
	[navigationController release];
	[window release];
	[super dealloc];
}
 
 
@end
<br />

Code:
// RootViewController.h
 
#import <UIKit/UIKit.h>
#import "SecondViewController.h"
 
@interface RootViewController : UIViewController {
	UILabel *display;
}
@property (nonatomic, retain) IBOutlet UILabel *display;
 
- (IBAction)pushViewController;
 
- (void)updateLabelWithString:(NSString *)buttonPressed;
 
@end
Code:
// RootViewController.m
 
#import "RootViewController.h"
// #import "SecondViewController.h"
 
 
@implementation RootViewController
@synthesize display;
 
#pragma mark -
#pragma mark Custom Methods
 
- (IBAction)pushViewController {
	SecondViewController *vc = [[SecondViewController alloc] init];
	[self.navigationController pushViewController:vc animated:YES];
}
 
- (void)updateLabelWithString:(NSString *)buttonPressed {
	self.display.text = buttonPressed;
}
 
#pragma mark -
#pragma mark View lifecycle
 
 
- (void)viewDidLoad {
	self.title = @"Root View";
	[super viewDidLoad];
 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
 
- (void)viewWillAppear:(BOOL)animated {
	[super viewWillAppear:animated];
}
 
 
#pragma mark -
#pragma mark Memory management
 
- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc that aren't in use.
}
 
- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
	self.display = nil;
}
 
 
- (void)dealloc {
	[display release];
    [super dealloc];
}
 
@end
<br />

Code:
// SecondViewController.h
#import <UIKit/UIKit.h>
 
 
@interface SecondViewController : UIViewController {
	UIButton *one;
	UIButton *two;
	UILabel *display;
	
	NSString *buttonPressed;
}
@property (nonatomic, retain) IBOutlet UIButton *one;
@property (nonatomic, retain) IBOutlet UIButton *two;
@property (nonatomic, retain) IBOutlet UILabel *display;
 
@property (nonatomic, retain) NSString *buttonPressed;
 
- (IBAction)buttonWasPressed:(id)sender;
 
@end
// SecondViewController.m

Code:
#import "SecondViewController.h"
#import "RootViewController.h"
 
@implementation SecondViewController
@synthesize one, two, buttonPressed, display;
 
- (IBAction)buttonWasPressed:(id)sender {
	if (sender == one) {
		display.text = @"Button one pressed.";
		self.buttonPressed = @"One";
	}
	else {
		display.text = @"Button two pressed.";
		self.buttonPressed = @"Two";
	}
}
 
- (void)updateStringWithString:(NSString *)string {
	RootViewController *rootController = (RootViewController *)self.navigationController.parentViewController;
	[rootController updateLabelWithString:string];
	NSLog (@"%s: rootController = %@", __func__, rootController);
}
 
 
- (void)viewDidLoad {
	self.title = @"Detail View";
    [super viewDidLoad];
	// Log (@"delegate is %@", self.delegate);
}
 
- (void)viewWillDisappear:(BOOL)animated {
	[self updateStringWithString:buttonPressed];
	[super viewWillDisappear:animated];
}
 
 
- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}
 
- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
	self.one = nil;
	self.two = nil;
	self.display = nil;
	self.buttonPressed = nil;
}
 
 
- (void)dealloc {
	[one release];
	[two release];
	[display release];
	[buttonPressed release];
    [super dealloc];
}
 
@end
<br />

I'd appreciate all the help I can get!
musicwind95 is offline   Reply With Quote
Old 07-05-2010, 04:55 PM   #24 (permalink)
Fly-by-night Innovator
 
Join Date: Jun 2010
Posts: 364
musicwind95 is on a distinguished road
Default

Bump...?

My app has grown a lot more involved...protocols and "simple" things like that probably aren't going to be very effective anymore.

That being said, how would I go about creating a singleton Global class? I'd probably put it in the App Delegate...what then?
musicwind95 is offline   Reply With Quote
Old 07-05-2010, 05:38 PM   #25 (permalink)
Nuisance Developer
 
Join Date: Jul 2009
Location: Italy
Posts: 4,691
dany_dev is on a distinguished road
Default

SynthesizeSingleton.h
ModelData.m
ModelData.h


Code:
//
//  SynthesizeSingleton.h
//  CocoaWithLove
//
//  Created by Matt Gallagher on 20/10/08.
//  Copyright 2009 Matt Gallagher. All rights reserved.
//
//  Permission is given to use this source code file without charge in any
//  project, commercial or otherwise, entirely at your risk, with the condition
//  that any redistribution (in part or whole) of source code must retain
//  this copyright and permission notice. Attribution in compiled projects is
//  appreciated but not required.
//

#define SYNTHESIZE_SINGLETON_FOR_CLASS(classname) \
\
static classname *shared##classname = nil; \
\
+ (classname *)shared##classname \
{ \
@synchronized(self) \
{ \
if (shared##classname == nil) \
{ \
shared##classname = [[self alloc] init]; \
} \
} \
\
return shared##classname; \
} \
\
+ (id)allocWithZone:(NSZone *)zone \
{ \
@synchronized(self) \
{ \
if (shared##classname == nil) \
{ \
shared##classname = [super allocWithZone:zone]; \
return shared##classname; \
} \
} \
\
return nil; \
} \
\
- (id)copyWithZone:(NSZone *)zone \
{ \
return self; \
} \
\
- (id)retain \
{ \
return self; \
} \
\
- (NSUInteger)retainCount \
{ \
return NSUIntegerMax; \
} \
\
- (void)release \
{ \
} \
\
- (id)autorelease \
{ \
return self; \
}
Code:
//
//  ModelData.h


#import <Foundation/Foundation.h>


@interface ModelData : NSObject {
	
	NSString *myMagicGlobalVariable;

	
}
+ (ModelData *)sharedModelData;



@property (nonatomic, retain) NSString *myMagicGlobalVariable;


@end
Code:
//
//  ModelData.m

#import "ModelData.h"
#import "SinthesizeSingleton.h"


@implementation ModelData

SYNTHESIZE_SINGLETON_FOR_CLASS(ModelData);

@synthesize myMagicGlobalVariable;

- (id) init{
	
	self = [super init];
	
	if (self != nil) {
		
		myMagicGlobalVariable= [[NSString alloc] initWithString:@"not bad!"];

	}
	
	return self;
}	


- (void) dealloc{
	[myMagicGlobalVariable release];

	[super dealloc];
	
}



@end

now import "ModelData.h" in each viewcontroller and then on your viewcontroller:
Code:
ModelData *myModelData = [ModelData sharedModelData];
NSLog(@"my shared string:@",myModelData.myMagicGlobalVariable);
__________________
dany_dev is offline   Reply With Quote
Reply

Bookmarks

Tags
data transfer, modal views, nav controller, properties

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: 331
9 members and 322 guests
Absentia, Chickenrig, Domele, fiftysixty, givensur, heshiming, michaelhansen, PixelInteractive, Sloshmonster
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,657
Threads: 94,118
Posts: 402,892
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jenniead38
Powered by vBadvanced CMPS v3.1.0

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