2016-05-27 6 views
0

ユーザが特定の場所にいるときに、私のアプリに警告表示を表示したい。また、私はこのアラートビューを1回だけ提示したいだけです。別のViewControllerへの移行時にリセットされる変数

if(!hasShownAlertview && GMSGeometryContainsLocation(userLocation.coordinate, testPath, YES)){ 
      hasShownAlertview = YES; 

      UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" [email protected]"Body" preferredStyle:UIAlertControllerStyleAlert]; 

      UIAlertAction *yesAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"YES", nil) style:UIAlertActionStyleDefault handler: 
              ^(UIAlertAction *action){ 
              }]; 

      UIAlertAction *noAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"NO", nil) style:UIAlertActionStyleDefault handler: 
             ^(UIAlertAction *action){ 
             }]; 

      [alertController addAction:noAction]; 
      [alertController addAction:yesAction]; 
      [self presentViewController:alertController animated:YES completion:nil]; 
    } 
} 

問題は次のとおりです:ユーザーがその場所にとどまっている間、私は、別のViewControllerに行けば、アラートビューは、そののViewControllerに再び示されている。このために、 は、私は次のコードを持っています。この理由は、新しいViewControllerが表示される直前にhasShownAlertviewがNOに設定されているため、アラートビューが再度表示されることです。

どうすればこの問題を解決できますか?

+0

hasShownAlertView変数を取得しますか? – Shreyank

+0

これはivファイルとして.hファイル内で宣言されています –

+0

'hasShownAlertview'の' Strong'を作成します – Shreyank

答えて

0

hasShownAlertviewappdelegateに宣言して、アプリ内のどこでもそれを使用して、はいまたはいいえかどうかを確認できます。そして、あなたはdeallocではないので、そのプロパティの強力な参照を取る必要があります。

+0

ありがとう、それはブールです、ブールの強い参照を持つことはできません –

+0

それから 'appdelegate'でそれを取ろうとします。もしあなたが'強い '参照を取りたいならば、代わりに' NSString'を取って 'yesまたはno 'それを' isequaltostring'と比較するか、 'NSInteger'を取るこ​​とができます – Lion

0

ファイルにhasShownAlertviewstaticと指定して修正しました。

0

変数hasShownAlertViewを複数のファイルに使用する場合は、このようにする必要があります。

Appelegate.h

、あなたが値を確認したり、変更したいすべてのファイルで

@property (assign) bool hasShownAlertView; 

をdelcare、あなたは `hasShownAlertview`はあなたが見ることができます宣言する方法

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 
bool hasShownAlertView = appDelegate.hasShownAlertView; 
関連する問題