Amazon.com Widgets question about global classes
Welcome, Guest. Please login or register.
Did you miss your activation email?
September 01, 2014, 04:25:23 PM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+ Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+ Old Stuff
| |-+ Chapter Study
| | |-+ Chapter 10 - More on Variables and Data Types
| | | |-+ question about global classes
Pages: [1] Go Down
Print
Author Topic: question about global classes (Read 1106 times)
emeraldMaster
Newbie
*
Posts: 2






on: January 21, 2011, 07:41:21 AM

hi all, I'm new to obj-C programming and I have a question.

in my application i need to login to server and store server response in class named UserData for example. in this class i have a lot of properties like session_id, user_id and others. I need to use this class data in different View controllers and Models, so I can't understand how I can send the data from UserData class to others? how can I get access to it ?
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #1 on: January 24, 2011, 10:31:27 AM

There are different ways to tackle this problem.  You can share variables through a common object, such as your application delegate, for example.   To access that object from other objects, you would first programmatically get the application delegate object:

Code: (Objective-C)
MyAppDelegate *myDelegate = (MyAppDelegate *) [[UIApplication sharedApplication] delegate];

Then you could execute any method in your app delegate class, or set or change an instance variable in the object.  So, for example, if you have an NSString variable called session_id and you wanted to assign it to a label from another class, you could write a sequence like this:

Code: (Objective-C)
MyAppDelegate *myDelegate = (MyAppDelegate *) [[UIApplication sharedApplication] delegate];

myLabel.text = [myDelegate session_id];

Cheers,

Steve Kochan
Last Edit: January 25, 2011, 07:23:59 PM by skochan Logged
emeraldMaster
Newbie
*
Posts: 2






Reply #2 on: January 25, 2011, 08:04:03 AM

thanks mr. Kochan for your reply!
I tried to do this, but my application goes to crash without any error message in console Sad
here is what I made:
created class named MyModel with session_id and others properties

then I went to my application delegate class and created new class attribute named "model":
Code: (Objective-C)
@interface DelegateTestAppDelegate : NSObject <UIApplicationDelegate> {
 UIWindow *window;
 IBOutlet UITabBarController *rootController;
 MyModel *model;
}

and two methods to set/get this property:
Code: (Objective-C)
- (MyModel *)getModel {
 return model;
}
- (void) setModel: (MyModel *)newModel {
 model = newModel;
}

then I tried to create two ViewController's to test it, in first I set MyModel property of delegate

first ViewController
Code: (Objective-C)
 
- (void)viewDidLoad {
 [super viewDidLoad];
 DelegateTestAppDelegate *delegate = (DelegateTestAppDelegate *) [[UIApplication sharedApplication] delegate];
 MyModel *model = [[MyModel alloc] init];
 (.. set some properties of model ..)
 [delegate setModel:model];
 [model release];
}

In second ViewController i get MyModel from delegate:
Code: (Objective-C)
- (void)viewDidLoad {
 [super viewDidLoad];
 MyModel *model;
 DelegateTestAppDelegate *delegate = (DelegateTestAppDelegate *) [[UIApplication sharedApplication] delegate];
 model = [delegate getModel];
}

when first controller loaded, application works fine. but when second controller loaded -- it crashes. where am I wrong?

ps. thanks for awesome obj-c 2.0 book and for this helpful forum!
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #3 on: January 25, 2011, 10:28:20 AM

You are releasing mode1 in your first view controller, so it's getting destroyed.  Either remove the release or define a property for the variable that retains it when the setter is used.

As an FYI, you don't want to use the word "get" in your getter.  That goes against Apple's naming conventions.  If you use that word, a user of the method assumes they must release the object that is returned (this is by Apple's convention).

Cheers,

Steve
Logged
Pages: [1] Go Up
Print
Jump to:



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.