简体   繁体   中英

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. 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 . I calling PB_TIMER_UIApplication class in main.m class it implementation as following,

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

I set principalClassName as a PB_TIMER_UIApplication

Now i create one property and instance method in PB_TIMER_UIApplication like following,

@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. That why i create an property for timerTimeOutIntervals and that property and resetIdleTimer instance method i want to call in throughout application. So i trying to access property/method as like Appdelegate call, [[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. 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:

 [(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. Due to this my UIApplication subclass run first then main UIApplication where UIApplicationDelegate are set. 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. I create my own custom UIWindow class and put up all timeout code as his given Github link code.

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. Note :- As per apple doc given UIResponder chain every touch calling it superview and finally call UIWindow.

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

This finally worked for me.

#define gkSDUIApplication (SDUIApplication *)[UIApplication sharedApplication]

SDUIApplication *myUIApplication = gkSDUIApplication;
myUIApplication.myProperty;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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