문제

    MyAppDelegate *appD;        
    appD = [UIApplication sharedApplication];

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

컴파일하는 동안 오류가 없습니다.경고없이 실행되지만 작동하지 않습니다.

문제가 무엇인지 보지 마십시오.

...

편집 : OMG는 델리게이트 방법이라고 했어야합니다 :

appD = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
.

도움이 되었습니까?

해결책

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

다른 팁

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.)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top