3
    MyAppDelegate *appD;        
    appD = [UIApplication sharedApplication];

    if(appD.sw1.on)
        NSLog(@"It is ON");
    else
        NSLog(@"It is OFF");

Gives no error while compiling. Runs without any warning, but it doesn't work.

I dont see what the problem is.

...

EDIT: OMG, should have called delegate method too:

appD = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
erkanyildiz
  • 13,044
  • 6
  • 50
  • 73

2 Answers2

2

I recommend you this method to share the app delegate: https://coderwall.com/p/z4h4uw?i=2&p=1&q=&t%5B%5D=%21%21mine&t%5B%5D=%21%21bookmarks

Sergi Gracia
  • 288
  • 1
  • 6
1

and instantiated an appDelegate …

No, you haven't instantiated anything, you've just declared a variable that can point to your app delegate. But you haven't assigned anything to that variable yet.

And instantiating another object would be wrong here since the app delegate instance already exists. You just have to reference the existing app delegate and assign it to your variable:

appD = (myAppDelegate *)[[UIApplication sharedApplication] delegate];

(Btw, you should follow the naming conventions. Class names should always begin with a capital letter.)

erkanyildiz
  • 13,044
  • 6
  • 50
  • 73
Ole Begemann
  • 135,006
  • 31
  • 278
  • 256
  • How would I know what exactly you need to type? You don't even tell me the names of your classes. The line I suggested above is correct. Put it in your class's init method. – Ole Begemann Feb 20 '11 at 01:26