-->

want to call ViewController's method from appd

2019-07-27 07:16发布

问题:

I want to call ViewController's method in AppDelegate.m. i have method method() in my Viewcontroller .i want it to be called when didSelectMethod() called in appdelegate.m. I have called method like this.-

ViewController *vc=[[ViewController alloc]init];
[vc method];

method is called but not having same instance as actually method having. it having all nill values. can anyone give me right code for this what i want.

Thank you Jagveer Rana

回答1:

While this has been correctly answered for the case where the view controller is the rootViewController, for completeness sake here's how you can get this to work with any view controller:

// ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

- (void)myMethod;

@end

// ViewController.m

#import "ViewController.h"
#import "AppDelegate.h"

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    appDelegate.myViewController = self;

}

- (void)myMethod
{
    NSLog(@"Doing something interesting");
}

// AppDelegate.h

#import <UIKit/UIKit.h>
#import "ViewController.h"

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (weak, nonatomic) ViewController *myViewController;

@end

// AppDelegate.m

#import "AppDelegate.h"

@implementation AppDelegate

- (void)applicationDidEnterBackground:(UIApplication *)application {

    [self.myViewController myMethod];

}


回答2:

The simplest way to get the same instance of anyViewController in your app is track it one by one... Like from any viewController in your app

[[[UIApplication sharedApplication] keyWindow] rootViewController];

or from appDelagate

self.window.rootViewController;

it will give you the rootViewController then from this rootViewController track your desired viewController.



回答3:

In your question you create a new view controller instance which is not part of the view hierarchy so you won't see any effect by calling the method (UI-wise). (Also it was not initialized via xib/storyboard so your UI elements will probably be nil)

You can access it via the window as long as it is the root view controller (otherwise you will have to find it or keep a reference to it when created):

ViewController *vc = (ViewController *)self.window.rootViewController;
[vc method]; 


回答4:

First you have declare your ViewController Class in Appdelegate.h and make an object of your UIViewController class in AppDelegate.h ,like this

@class yourViewControllerClass;
@property (nonatomic,strong) yourViewControllerClass *obj1;

Now import your ViewController class in AppDelegate.m, like this

#import yourViewControllerClass.h;

Now in AppDeledate.m 's method applicationDidFinishLaunchingOption: create a new object of your viewController and assign it to obj1 ,like this

yourViewControllerClass *obj2 = [[yourViewControllerClass alloc]init];
obj2 = self.obj1 

With help of this code you can parse data between viewcontrollers or objects..Now in your ViewController Class's .m file you have to import Appdelegate.h and parse all data from your object of viewController to obj1 and obj1 will parse that data to obj2 (with the helo of above code).[Assuming object of your viewController class as OBJ].

#import AppDelegate.h
AppDelegate *ad = [[AppDelegate alloc]init];
ad.obj1 = OBJ

NOTE- i haven't tested this code..please save your project first somewhere else..answered according to my knowledge..hope this will help you..Thank you



回答5:

In the .pch file write this code

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

In Any ViewController you want to call Delegate's method then simply write this code.

[DELEGATE methodname];