Jag*_*ngh 1 methods objective-c uiviewcontroller ios appdelegate
我想ViewController在 AppDelegate.m 中调用的方法。我的方法中有 method() 。我Viewcontroller希望在调用 didSelectMethod() 时调用它appdelegate.m。我已经调用了这样的方法。-
ViewController *vc=[[ViewController alloc]init];
[vc method];
Run Code Online (Sandbox Code Playgroud)
方法被调用但与实际方法具有不同的实例。它具有所有nill价值。谁能给我正确的代码,我想要什么。
谢谢贾格维尔·拉纳
虽然在视图控制器是 的情况下已经正确回答了这个问题rootViewController,但为了完整起见,这里是如何让它与任何视图控制器一起工作:
// 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];
}
Run Code Online (Sandbox Code Playgroud)