Objective c AppDelegate从m文件调用函数+;presentViewController
我有一个Objective c AppDelegate从m文件调用函数+;presentViewController,objective-c,appdelegate,Objective C,Appdelegate,我有一个AdIntegrator.mm文件,其中包含一个创建表单的函数: -(void)createAndDisplayForm{ AppDelegate* app = (AppDelegate*)[[UIApplication sharedApplication] delegate]; //add AppDelegate pointer _vc = app.window.rootViewController; //create form
AdIntegrator.mm
文件,其中包含一个创建表单的函数:
-(void)createAndDisplayForm{
AppDelegate* app = (AppDelegate*)[[UIApplication sharedApplication] delegate]; //add AppDelegate pointer
_vc = app.window.rootViewController;
//create form
//presented using presentFromViewController:_vc
}
我希望能够在需要时从我的AppDelegate.mm
再次使用此表单,(如果用户
希望再次查看此表单)
我怎么称呼这个
我从我的AppDelegate.mm
尝试了以下操作,但未显示任何视图:
AdIntegrator* opt = [[AdIntegrator alloc] init];
[opt createAndDisplayForm];
有谁能告诉我从AppDelegate使用此函数的最佳方法,以及确保可以再次看到presentFromViewController。谢谢。让我解释一下,AppDelegate是一个对象,因此您可以在AppDelegate.h中声明AdIntegrator类,如下所示:
#import "AdIntegrator.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (nonatomic) AdIntegrator *adIntegratorObject;
@end
\u AdIntegrator对象是AppDelegate单例中的引用
要从任何地方调用该方法,您可以从OtherViewController.m执行此操作,您需要#在OtherViewController.m中导入“AppDelegate.h”,以查看对象并调用该方法:
- (void)methodExampleFromOtherViewController
{
AppDelegate *appObject = (AppDelegate*)[[UIApplication sharedApplication] delegate];
// Calling your method in adIntegratorObject class
[appObject.adIntegratorObject YOUR_METHOD_CALL];
}
您可以使用presentViewController:animated:completion:方法,从AppDelegatecheers:)调用AdIntegrator对象。谢谢您的帮助。代码可以编译,但出于某种原因,它没有调用函数。我在一开始就放了一个
NSLog
,但没走多远。我还必须使用[AdIntegrator new]代码>我将AppDelegate.h文件修改为#import“AdIntegrator.h”,现在该方法是可见的,对不起,我的朋友,我希望它能帮助你:)以后你可以创建自己的singleton类,像这样在AppDelegate.h singleton中声明,并在该对象中使用其他类,因为您不想在AppDelegate.h:D中导入许多类
- (void)methodExampleFromOtherViewController
{
AppDelegate *appObject = (AppDelegate*)[[UIApplication sharedApplication] delegate];
// Calling your method in adIntegratorObject class
[appObject.adIntegratorObject YOUR_METHOD_CALL];
}