最好的方式是在NSObject的UIWindow上隐藏UIView?

时间:2014-02-06 04:16:37

标签: ios objective-c uiview nsobject uiwindow

我有一个处理位置的NSObject类,在特定的点上它会显示一个UIView并不断更新视图然后隐藏它,反之亦然。

我尝试了addSubview的几种方法:

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

我在这个UIView上面有一个UILabel,我更新了一些值(点到点之间的距离),不知何故它不再更新视图,它只显示一次,就是它并且它不会隐藏它回来了。

我的代码:

// 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];
}

我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

如果您想简单地展示和关闭视图,请使用presentViewControllerdismissViewController。不建议经常更改keyWindow