Executing method within appDelegate class from viewController class

Discussion in 'iOS Programming' started by Xino, Nov 13, 2008.

  1. Xino macrumors member

    Sep 5, 2008
    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.
  2. lawicko macrumors member

    Jul 17, 2008
    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.
  3. Xino thread starter macrumors member

    Sep 5, 2008
    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.
  4. Xino thread starter macrumors member

    Sep 5, 2008
    Ok I fixed the errors occurred by changing the code to:

    #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:

  5. Xino thread starter macrumors member

    Sep 5, 2008
    Nevermind, this error was caused by an invalid interface builder item.

Share This Page