2016-05-27 24 views
0

我想在用戶位於特定位置時在我的應用中顯示警報視圖。另外,我只想呈現一次警報視圖。對於這一點,我 有下面的代碼:過渡到另一個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出現之前hasShownAlertview被設置回NO,因此警報視圖再次顯示。

我怎麼能解決這個問題?

+0

你如何聲明'hasShownAlertview'可以告訴你? – Shreyank

+0

它在.h文件中聲明爲ivar –

+0

使屬性強'hasShownAlertview'' – Shreyank

回答

0

您可以在appdelegate中聲明hasShownAlertview,並可以在應用程序的任何位置使用它來檢查它是否爲yes或no。而且你應該強調這個屬性,所以它不是dealloc。

+0

謝謝,但它是一個Bool,不能有強烈的參考bools –

+0

然後嘗試把它放在'appdelegate'中,如果你想採用'strong'引用,那麼你可以採用'NSString'而不是bool並設置yes或no '並與'isequaltostring'比較,或者你可以帶'NSInteger' – Lion

0

通過在.m文件中聲明hasShownAlertviewstatic來修復它。

0

如果你想在多個文件中使用靜態的hasShownAlertView變量,你應該這樣做。

Appelegate.h,delcare

@property (assign) bool hasShownAlertView; 

在您要檢查或更改值的任何文件,你會得到hasShownAlertView變量由

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 
bool hasShownAlertView = appDelegate.hasShownAlertView; 
相關問題