PDA

View Full Version : Executing method within appDelegate class from viewController class




Xino
Nov 13, 2008, 07:28 AM
I have an application with a class named "appliationAppDelegate" which is the delegate of my application. Most function are within that class.

I also have the class applicationViewController because the method 'didReceiveMemoryWarning' must be inside this file.

How can I execute a method that is located in the class 'appliationAppDelegate' from the method 'didReceiveMemoryWarning' which is located in 'applicationViewController'?

Tried everything I could think of but still no good results...

Thanks for your help :)

PS. I've created those classes with Interface Builder.



lawicko
Nov 13, 2008, 08:56 AM
You can access your delegate from everywhere in your app this way:
[UIApplication sharedApplication].delegate
Make sure you cast it to your delegate class to gain access to it's properties and other functionalities.
But to be honest - this is not the best way to do things in object oriented environment. Better try to create protocol for your view controller and then implement this protocol in your app delegate class, this way you can easily get any kind of notifications from view controller to your app delegate if you need.

Xino
Nov 14, 2008, 03:26 AM
Make sure you cast it to your delegate class to gain access to it's properties and other functionalities.

What do you mean by that?

Because this doesn't work:
[[UIApplication sharedApplication].delegate resetMonitorBecauseOfMemoryError];

You're right about the fact this isn't the best way to code but I'm kind of new in OOP developing. When the application works, I will change the code to be OOP.

Xino
Nov 14, 2008, 03:56 AM
Ok I fixed the errors occurred by changing the code to:

monitorViewController.m

#import "Dyna_VisionAppDelegate.h"
...
- (void)didReceiveMemoryWarning
{
NSLog(@"[INFO] didReceiveMemoryWarning method started");
// Reset monitor
Dyna_VisionAppDelegate *appDelegate = (Dyna_VisionAppDelegate*)[[UIApplication sharedApplication] delegate];
[appDelegate resetMonitorView];
}


The application compiles perfectly but when I execute it, the application is interrupted and in my console I see the following error message:


*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<monitorViewController 0x33d500> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key appDelegate.'
2008-11-14 10:54:23.277 Dyna-Vision[190:20b] Stack: (
808221155,
806100820,
808083569,
812534289,
812167707,
812166769,
812380957,
818171664,
807885467,
807923097,
818165960,
818174556,
816144784,
816178204,
812207357,
807837135,
807834471,
827655876,
816145636,
816184028,
8381,
8244
)
terminate called after throwing an instance of 'NSException'

Xino
Nov 14, 2008, 04:06 AM
Nevermind, this error was caused by an invalid interface builder item.