title: Call a method from AppDelegate - Objective-C
body: <p>I was trying to call an existing method of my <code>ViewController.m</code> from <code>AppDelegate.m</code> inside the         <code>applicationDidEnterBackground</code> method, so I found this link: <a href="https://stackoverflow.com/questions/18950670/calling-uiviewcontroller-method-from-app-delegate">Calling UIViewController method from app delegate</a>, which told me to implement this code:</p>
  
  <p>In my ViewController.m</p>
  
  <pre><code>-(void)viewDidLoad
  {
      [super viewDidLoad];
  
      AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
      appDelegate.myViewController = self;
  }
  </code></pre>
  
  <p>In my AppDelegate:</p>
  
  <pre><code>@class MyViewController;
  
  @interface AppDelegate : UIResponder &lt;UIApplicationDelegate&gt;
  
  @property (weak, nonatomic) MyViewController *myViewController;
  
  @end
  </code></pre>
  
  <p>And in the AppDelegate's implementation:</p>
  
  <pre><code>- (void)applicationDidEnterBackground:(UIApplication *)application
  {
      [self.myViewController method];
  }
  </code></pre>
  
  <p>So I put this code in my project and it worked fine, but I didn't understand how the code works, line by line. What does the <code>sharedApplication</code> do? Why must I set a delegate instead of just creating an instance of ViewController, like:</p>
  
  <pre><code>ViewController * instance = [[ViewController alloc] init];
  [instance method];
  </code></pre>