好吧我對IOS開發很陌生,但我正在編寫一個應用程序,我正在使用計時器類來超時用戶,如果他們在故事板中的任何特定場景上閒置時間過長,並且會使用戶回到原始場景/視圖。我有一個由多個場景/視圖組成的單個故事板(不知道這裏的正確單詞是什麼),並且每個場景都有它自己的視圖控制器。Xcode IOS - 如何獲得當前在appdelegate中查看的場景/視圖
我通過appdelegate類完成了超時。見下面的代碼。
所以我有代碼工作,它的工作原理很好,但我試圖讓它會忽略計時器,如果我們在主場景。
我已經使用了這個,閱讀了大量的文檔,並嘗試了很多東西,但到目前爲止我還沒有能夠弄清楚如何在applicationDidTimeout方法中獲取當前查看的場景。
如果我能得到當前查看的場景/視圖的名稱,那麼我可以選擇忽略定時器。
有誰知道如何做到這一點?
謝謝你的時間。
#import "StoryboardAppDelegate.h"
#import "TIMERUIApplication.h"
@implementation StoryboardAppDelegate
@synthesize window = _window;
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// applicaiton has timed out
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidTimeout:) name:kApplicationDidTimeoutNotification object:nil];
return YES;
}
-(void)applicationDidTimeout:(NSNotification *) notif
{
NSLog (@"time exceeded!!");
UIViewController *controller = [[UIStoryboard storyboardWithName:@"Main" bundle:NULL] instantiateViewControllerWithIdentifier:@"StoryboardViewController"];
UINavigationController * navigation = [[UINavigationController alloc]initWithRootViewController:controller];
[self.window setRootViewController:navigation];
navigation.delegate = self;
navigation.navigationBarHidden = YES;
if (controller) {
@try {
[navigation pushViewController:controller animated:NO];
} @catch (NSException * ex) {
//「Pushing the same view controller instance more than once is not supported」
//NSInvalidArgumentException
NSLog(@"Exception: [%@]:%@",[ex class], ex);
NSLog(@"ex.name:'%@'", ex.name);
NSLog(@"ex.reason:'%@'", ex.reason);
//Full error includes class pointer address so only care if it starts with this error
NSRange range = [ex.reason rangeOfString:@"Pushing the same view controller instance more than once is not supported"];
if ([ex.name isEqualToString:@"NSInvalidArgumentException"] &&
range.location != NSNotFound) {
//view controller already exists in the stack - just pop back to it
[navigation popToViewController:controller animated:NO];
} else {
NSLog(@"ERROR:UNHANDLED EXCEPTION TYPE:%@", ex);
}
} @finally {
//NSLog(@"finally");
}
} else {
NSLog(@"ERROR:pushViewController: viewController is nil");
}
[(TIMERUIApplication *)[UIApplication sharedApplication] resetIdleTimer];
}
@end
那麼,我知道獲取「當前查看視圖」的一種方法是獲取頂部窗口。你可以通過說UIViewController * topView = self.window.subViews [0]; – user2277872
好的,我會試試這個謝謝! – ChiquitaLord
好的,當我在調試模式下訪問topView並查看它的屬性時,它並沒有給我任何有用的信息來識別它。例如,它的標題是零。 一旦我有topview我需要做什麼來弄清楚我在哪個場景? 再次感謝您的幫助。 – ChiquitaLord