简体   繁体   English

从Objective-C中的主类调用方法

[英]Calling method from principal class in objective-C

I'm did functionality of timer out if User can't touch on screen for 10 minutes then my application directly goes on login screen. 如果用户在10分钟内无法触摸屏幕,那么我就完成了超时功能,然后我的应用程序直接进入了登录屏幕。 For above problem i use sample code from here https://github.com/B-Sides/ELCUIApplication doing some changes in naming of class so my class is PB_TIMER_UIApplication instead of ELCUIApplication . 对于上述问题,我使用来自此处的示例代码https://github.com/B-Sides/ELCUIApplication对类的命名进行了一些更改,因此我的类是PB_TIMER_UIApplication而不是ELCUIApplication I calling PB_TIMER_UIApplication class in main.m class it implementation as following, 我在main.m类中调用PB_TIMER_UIApplication类,实现如下,

return UIApplicationMain(argc, argv, NSStringFromClass([PB_TIMER_UIApplication class]), NSStringFromClass([AppDelegate class]));

I set principalClassName as a PB_TIMER_UIApplication 我将PrincipalClassName设置为PB_TIMER_UIApplication

Now i create one property and instance method in PB_TIMER_UIApplication like following, 现在,我在PB_TIMER_UIApplication创建一个属性和实例方法,如下所示,

@interface PB_TIMER_UIApplication :  UIApplication{
    NSTimer *_idleTimer;
}
@property(nonatomic) int timerTimeOutIntervals;

- (void)resetIdleTimer;

Now i want my application show alert before going login screen/timeout session. 现在,我希望我的应用程序在进入登录屏幕/超时会话之前显示警报。 If alert button press OK then my timer again start for 10 second. 如果警报按钮按OK,则我的计时器再次启动10秒钟。 That why i create an property for timerTimeOutIntervals and that property and resetIdleTimer instance method i want to call in throughout application. 这就是为什么我要为timerTimeOutIntervals创建一个属性,而我想在整个应用程序中调用该属性和resetIdleTimer实例方法的原因。 So i trying to access property/method as like Appdelegate call, [[PB_TIMER_UIApplication sharedApplication] setTimerTimeOutIntervals:10]; 所以我试图像Appdelegate调用一样访问属性/方法, [[PB_TIMER_UIApplication sharedApplication] setTimerTimeOutIntervals:10]; but it show me static error , 但是它告诉我静态错误,

在此处输入图片说明

My question where i'm wrong and what to do for calling property of instance method from principle class? 我的问题是我错了,如何从原则类调用实例方法的属性?

Your class should be inherited from UIREsponder. 您的课程应该从UIREsponder继承。 For example: 例如:

 MyAppDelegate:UIResponder <UIApplicationDelegate>

And you should use this way for get access for it: 并且您应该使用这种方式来获取访问权限:

 [[[UIApplication sharedApplication] delegate] setTimerTimeOutIntervals:10];

If you want call your methods then try: 如果要调用方法,请尝试:

 [(MyAppDelegate*)[[UIApplciation sharedApplication] delegate] someMethod];

UPD: You can try use it: UPD:您可以尝试使用它:

 [(PB_TIMER_UIApplication*)[UIApplication sharedApplication] setTimerTimeOutIntervals:10];

Yes i solve my problem. 是的,我解决了我的问题。 I changed my architecture of application firstly i create subclass of UIApplication and and set it as a principle class in main.m file. 我首先更改了应用程序的体系结构,然后创建了UIApplication子类,并将其设置为main.m文件中的主体类。 Due to this my UIApplication subclass run first then main UIApplication where UIApplicationDelegate are set. 因此,我的UIApplication子类首先运行,然后运行设置UIApplicationDelegateUIApplication And that why i unable declare launching timeout intervals, my app goes in recursion. 这就是为什么我无法声明启动超时间隔的原因,我的应用程序会递归运行。

After searching i got this link. 搜索后,我得到了这个链接。 I follow suggestion of @Brian King in that link. 我在该链接中遵循@Brian King的建议。 I create my own custom UIWindow class and put up all timeout code as his given Github link code. 我创建了自己的自定义UIWindow类,并将所有超时代码设置为他给定的Github链接代码。

Now my app architect is i put my rootviewcontroller on my custom UIWindow class, then show my custom alert view and write timeout/continue timer functionality on alert view buttons. 现在,我的应用架构师是将rootviewcontroller放在自定义UIWindow类上,然后显示我的自定义警报视图,并在警报视图按钮上编写超时/继续计时器功能。 Note :- As per apple doc given UIResponder chain every touch calling it superview and finally call UIWindow. 注意:-根据给苹果的文档,UIResponder链上的每一次触摸都将其称为超级视图,最后调用UIWindow。

根据您的要求,如果事件触摸阶段类型为UITouchPhaseBegan,我建议您在应用程序的AppDelegate和该重置计时器中使用sendEvent:(UIEvent *)event方法。

This finally worked for me. 这终于对我有用。

#define gkSDUIApplication (SDUIApplication *)[UIApplication sharedApplication]

SDUIApplication *myUIApplication = gkSDUIApplication;
myUIApplication.myProperty;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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