想从appdelegate调用ViewController的方法 [英] want to call ViewController's method from appdelegate
问题描述
我想在AppDelegate.m中调用ViewController
的方法.
我在Viewcontroller
中有method()方法.我希望在appdelegate.m
中调用didSelectMethod()时调用它.
我已经这样调用了方法.-
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];
调用了
方法,但没有与实际具有的方法相同的实例.
它具有所有nill
值.谁能给我适合我想要的代码.
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.
谢谢 Jagveer Rana
Thank you Jagveer Rana
推荐答案
尽管对于视图控制器为rootViewController
的情况,此问题已正确回答,但出于完整性考虑,这是使该视图与任何视图一起使用的方法控制器:
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];
}
这篇关于想从appdelegate调用ViewController的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!