2016-12-07 26 views
0

我正在使用以下代碼來顯示警報控制器,但有時它會收到上述消息並且警報控制器會停止響應。這是什麼意思?固定?嘗試在*上呈現*(已存在)(空)

+(void)showAlertFor:(UIViewController *)viewController Title:(NSString*)title WithMessage:(NSString *)message 
{ 

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Error" message:message preferredStyle:UIAlertControllerStyleAlert]; //Getting the alert view controller 
    UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { 

    }]; 

    [alertController addAction:ok]; 

    [viewController presentViewController:alertController animated:YES completion:nil]; 
} 
+0

檢查您是否提供多個控制器。 –

+0

這可以怎麼做? – TestShroff

+0

@TestShroff你可以從同一個地方的兩個地方調用'showAlertFor'嗎?或者同時解僱VC? – PiyushRathi

回答

0

試試這個

+(void)showAlertFor:(UIViewController *)viewController withTitle:(NSString*)title withMessage:(NSString *)message { 

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Error" message:message preferredStyle:UIAlertControllerStyleAlert]; //Getting the alert view controller 
    UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { 

    }]; 

    [alertController addAction:ok]; 
    dispatch_async(dispatch_get_main_queue(),^{ 
    [viewController presentViewController:alertController animated:YES completion:nil]; 
    }); 
} 

調用它的主線程將解決你的問題。並確保你沒有在同一時間出示任何其他控制器。

+0

它現在給出以下警告,並仍然會導致問題「嘗試加載視圖控制器的視圖時,它是釋放是不允許的,並可能導致未定義的行爲」。如何解決這個問題? – TestShroff

相關問題