2014-02-06 26 views
1

我有它處理的位置,並在特定的點它顯示了一個UIView,不斷更新視圖,然後隱藏它,反之亦然NSObject類。從NSObject的UIWindow頂部呈現和隱藏​​UIView的最佳方式?

我嘗試了一些方法與addSubview

[UIApplication sharedApplication].keyWindow 
[UIApplication sharedApplication].windows.firstObject 

我有一個UILabel在此的UIView我有一些價值的更新(從點距離點)之上,不知何故,不更新再看一遍,它顯示了一次,那就是它,它不會隱藏它。

我的代碼:

// Updates the UIView from NSObject class: 
NSMutableDictionary *params = [[NSMutableDictionary alloc] init]; 
      params[@"distance"] = @(distanceToTrap); 
      params[@"TrapType"] = @(trap.getTrapType); 
      params[@"TrapID"] = @(trap.getTrapID); 

      trapAlarmActivity = [[TrapAlarm alloc] initWithUserInfo:params]; 
      [trapAlarmActivity setNeedsDisplay]; 
      [trapAlarmActivity.screenView setNeedsDisplay]; 

      [[UIApplication sharedApplication].keyWindow addSubview:trapAlarmActivity]; 
      [[UIApplication sharedApplication].keyWindow setNeedsDisplay]; 

// The function inside the UIView that updates the UILabel and it somehow doesn't updates anymore 
- (void)broadcastLocationChange:(NSNotification*)notification 
{ 
    if (![db isTrapAlarmOpen]) { return; } 

    dispatch_async(dispatch_get_main_queue(), ^{ 
     if (globalTrap == nil) { 
      //globalTrap = [db getTrap_trapID:trapID]; 
      globalTrap = [dbat getTrap_trapID:trapID]; 
     } 


     int distanceToTrap = [db getTrapDistance:globalTrap.getTrapID]; 

     if (distanceToTrap == 0) { 
      CLLocation *originalLocation = kSERVICE_CONTEXT.locationManager.location; 
      CLLocation *trapLocation = [[CLLocation alloc] initWithLatitude:globalTrap.lat longitude:globalTrap.lon]; 

      distanceToTrap = [trapLocation distanceFromLocation:originalLocation]; 
     } 

     NSLog(@"TRAP ALARM - TRAP DISTANCE TO CAR: %i", distanceToTrap); 

     if (stillNotYetKey) 
     { 
      [self changeMessageBoxWithDistance:distanceToTrap]; 

      if (distanceToTrap < kFINISH_ALARM_DISTANCE) 
       stillNotYetKey = NO; 
      else 
      { 
       if (distanceToTrap > lastDistanceToTrap) { 
        [self finishTrapAlarm]; 
       } 
       else { 
        [self changeMessageBoxWithDistance:distanceToTrap]; 
       } 
      } 
     } 
     // Check if user exit polygon before passing trap 
     if (distanceToTrap > lastDistanceToTrap) 
     { 
      distanceGrow++; 
      if (distanceGrow >= kCOUNTER_FOR_CLOSE_WINDOW_WHEN_DISTANCE_GROW_USER_OUT) { 
       [self finishTrapAlarm]; 
      } 
     } 
     lastDistanceToTrap = distanceToTrap; 
     [self setNeedsDisplay]; 
     [[UIApplication sharedApplication].keyWindow setNeedsDisplay]; 
    }); 
} 

// Function that removes the UIView 
- (void)dismiss 
{ 
    [self removeFromSuperview]; 
    [[UIApplication sharedApplication].keyWindow setNeedsLayout]; 
} 

我做錯事會在這裏?

回答

0

如果你想簡單的現在和解僱視圖,則使用presentViewControllerdismissViewController的一樣。不建議經常更改keyWindow

+0

最終,無論其UIView或UIViewController我需要它從NSObject類顯示。 –

+0

@IdanMoshe您可以隨時設置委託函數來在UIViewController和NSObject類之間進行通信。使用'呈現的UIVIewController'顯示動態內容與您目前正在做的事情沒有多大區別。 –

相關問題