Executing method within appDelegate class from viewController class

Discussion in 'iPhone/iPad Programming' started by Xino, Nov 13, 2008.

  1. macrumors member

    Joined:
    Sep 5, 2008
    #1
    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. macrumors member

    Joined:
    Jul 17, 2008
    #2
    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. thread starter macrumors member

    Joined:
    Sep 5, 2008
    #3
    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. thread starter macrumors member

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

    monitorViewController.m
    Code:
    #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. thread starter macrumors member

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

Share This Page