我有一個類A聲明一個初始化函數,如下所示。這是從AppDelegate applicationDidBecomeActive
調用的。iOS位置變化的調用函數拋出異常
現在,當應用程序轉到後臺時,我正在捕獲用戶的當前位置。當應用程序進入前臺時,我得到新的位置,如果用戶移動超過給定距離,我想再次調用初始化,這會彈出mainviewcontroller
並重新計算數據。
我在行mvc = ...
中發現異常。如果我將「應用程序不在後臺運行」設置爲YES
,則初始化調用沒有問題(如果我刪除了基於距離調用初始化)。任何幫助將非常感激!
的例外是:
[_UIModalItemAppViewController viewControllers]: unrecognized selector sent to instance
我:
....
UINavigationController *n ;
MainViewController *mvc;
....
....
+(void)refetch
{
static BOOL initialized = NO;
n = (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;
mvc = (MainViewController *)[n viewControllers][0];
if (!initialized)
{
initialized = YES;
calculatedValuesArray = [[NSMutableArray alloc] init];
[self updateLocation];
[self fetchData];
}
}
千萬不要自己調用'initialize'。這是一個標準的Objective-C方法,當運行時第一次引用該類時,該方法將被調用。 – rmaddy
謝謝,你是否認爲將重新計算移至另一個函數並調用該函數就足夠了? – rkh
或許。但從錯誤看來,'rootViewController'不是你認爲的那樣。看起來如果你的應用程序在後臺加載,'rootViewController'是一些私人類,而不是你期望的導航控制器。 – rmaddy