简体   繁体   中英

How to call an instance method of a different class from AppDelegate?

I have designed a web service application and fetch app's data from my server via some GET requests.. In my login class i enter userdefaults and save them to NSUSerDefaults, then in viewDidLoad of my login class i made a control, if the userdefaults are saved, i assing them to the text property of textfield. What i want is to call that method(login method) of my login class from the applicationDidFinishLaunchingWithOptions delegate method. Below is some peoudo code:

//LoginClass.m
@implementation LoginClass
....
//needed things...

-(ibaction)LoginMethod{
//the assingments&data fetchings ect

}

And i want to call this method in my appdelegate class like below

 //....Appdelegate class
    #import "LoginClass.h"
    //.....other things....
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
      LoginClass *myObj= [[LoginClass alloc]init];
       [myObj LoginMethod];
    }

How can i do this? Shortly over all, how can i call a method of other classes from AppDelegate class? Note: I use NSURLConnections and its delegate methods to fetch data, handle errors ect.. NOTE-2: I tagged with NSUserDefaults too because i use it too(in controlling i control its content)

EDIT After controlling defaults in my loginViewController's viewDidLoad, if there is a value in defaults i want to automatically call my login action, with no need to user's clicks.

Since the AppDelegate is usually the first class created this is where you can create the objects that you want to send messages. If this object creates them, then this objects knows about them and it can send it messages.

A word of caution, though:

  • Loading everything in your AppDelegate is going to take time. If you take too long then the Springboard will terminate your app.
  • I also think your design is a bit unsophisticated. If you are going to create everything in the AppDelegate then I suspect the control flow will be bouncing back to the AppDelegate to get references to other objects. Which doesn't make for modular code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM