Call a function in AppDelegate?

After solving (at the highest level of response) using the UITextField example in Cocos2d, I managed to do this, except for the line

[[[UIApplication sharedApplication] delegate] specifyStartLevel]; 

I placed it in my scene, I get this warning:

Instance method '-specifyStartLevel' not found (default return type is 'id')

Why? I explicitly have -specifyStartLevel defined in the header and implementation of my AppDelegate ...


Edit : defineStartLevel declaration

 #import <UIKit/UIKit.h> @class RootViewController; @interface AppDelegate : NSObject <UIApplicationDelegate,UITextFieldDelegate> { UIWindow *window; UITextField *levelEntryTextField; RootViewController *viewController; } - (void)specifyStartLevel; @property (nonatomic, retain) UIWindow *window; @end 

And implementation:

 - (void)specifyStartLevel { [levelEntryTextField setText:@""]; [window addSubview:levelEntryTextField]; [levelEntryTextField becomeFirstResponder]; } 
+4
source share
4 answers

Right now, your class knows nothing about your delegate methods. You need to import the delegate into your implementation, not your interface (to avoid circular import).

For instance,

 #import "AppDelegate.h" 

Then you should give the returned delegate to your nested method call as your delegate type. For instance:

 [(AppDelegate *)[[UIApplication sharedApplication] delegate] specifyStartLevel]; 
+14
source

1. Add your method to the AppDelegate.h file. For instance,

  - (void)Welcome 

2. Implement the method in the AppDelegate.m file. For example,

  - (void)Welcome { NSLog(@"Welcome") } 

3. Set the UIApplication delegate in the method, for example

  AppDelegate *appDelegate=[UIApplication sharedApplication] delegate]; [appDelegate Welcome]; 
+4
source

You need to import AppDelegate into the .m file where you use

 [[[UIApplication sharedApplication] delegate] specifyStartLevel]; 

I like to import it with a title that gives me a shortcut.

 GlobalData.h #import "AppDelegate.h" #define APPDELEGATE (AppDelegate *)[[UIApplication sharedApplication] delegate] 

Then when I use it in any class, I just

 #import "GlobalData.h" // to gain access to the delegate AppDelegate * appDelegate = APPDELEGATE; 

I use this approach because then I can store more #define for some global constants (i.e. soundFXVolume - #define SOUND_FX_V 0.6 )

+3
source

-[UIApplication delegate] returns an object of type id<UIApplicationDelegate> , so the compiler knows only those methods that objects of this type react to, even if your user delegate responds to specifyStartLevel . You can either ignore the warning or specify a return value -[UIApplication delegate] :

 [(YourCustomAppDelegate *) [[UIApplication sharedApplication] delegate] specifyStartLevel]; 
+3
source

Source: https://habr.com/ru/post/1382571/


All Articles