Reputation: 3718
This is kind of hackneyed question, I have some experience programming, but Objective-c is totally new for me, and I can't find the any clear definition for me about whole objective-c concept. Here some theory that I already know: I cannot access to instance variable of the class directly, thus I must use the property or "accessor methods" for access to it. Okay. I tried some of that I mean by accessor methods but hmmm it's all wrong. I think you can help me to figure out with this.
Here's my example: I have class Cat, and a plist that have info about a cat. Plist stores name and age of the pet. I want to made this: simple view that have textField, label, and a button. Label - is a current pet name, textField is for change the cat name for one that in textfield now method (changing cat name sounds creepy but this only an example), and a button to send message to the change name function. After I push a home button I want to save the name to file through AppDelegate.m file and that's a problem, I'll describe below.
I don't know how to wrap code into a block that is hide until you open it, so I don't want bother your eyes right here with a huge amount of code lines. I add a link to the code and some screens with log output. (If somebody will say me how I can do it, I'll add code here), but for now here is the link: http://shlonger.com/680dc6cf21aa426c4bed107213ab4467
I can't access to the object that implement when the app is working, from the AppDelegate. I made some properties and some getter/setter methods for class variables but again when I initialize Cat class in AppDelegate, it makes just another object with default (like in plist file) settings. How can I save current settings through the AppDelegate in a right way?
Sorry for a long question description.
Upvotes: 0
Views: 3804
Reputation: 4946
You do not necessarily need to commmunicate with the AppDelegate
, but you can via [[UIApplication sharedApplication] delegate]
. You can then access what ever properties you need, but you will probabaly need to cast it:
// assuming your application delegate is of type 'AppDelegate' - usually the default provided by Xcode
AppDelegate *delegate = (AppDelegate*)[[UIApplication shared application] delegate];
// you can now all you application delegat's properties.
Another method, in your Cat
class, is to register for notifications posted by the app delegate. For example:
@implementation Cat
-(id)init {
if ( (self = [super init]) ) {
// you can also attempt to read stored values form disk here
// perform all your other initialization that you already have
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleNotification:)
name:UIApplicationDidEnterBackgroundNotification
obect:[UIApplication sharedApplication];
}
}
In your Cat
class, define a method handleNotification:
, since that is selector provided to the notification center. This is the method the notification center will call on your Cat
class when UIApplicationDidEnterBackgroundNotification
is posted:
-(void)handleNotification:(NSNotification*)notification {
NSString *name = notification.name;
id sender = notification.object;
if ( [name isEqual:UIApplicationDidEnterBackgroundNotification] && [sender isEqual:[UIApplication sharedApplication] ) {
// save `Cat` class to disk as plist or however you want
}
}
You could also substitute UIApplicationDidEnterBackgroundNotification
with UIApplicationWillResignActiveNotification
. I will leave it up to you decide which best suit your needs.
More on using the NSNotificationCenter
.
UPDATE:
The advantage of the second method is there is no need for any specfic knowledge or dependancy between your Cat
class and the application delegate. Both objects can operate independently of each other. The [UIApplication sharedApplication]
is a singleton available to any object running in an iOS application, but again, you do not need to know the exact type. The only specific knowledge needed are the notification names, but even those are globally accessible.
Upvotes: 3