0

我刚刚学习了 IOS 编程并正在编写我的第一个 iPhone 应用程序。

我的应用程序提供有关 MKMapView 的信息,其视图控制器是 UINavigationController 的根视图控制器。如果移动信号不好,我使用 mapViewDidFailLoadingMap:withError: 使应用程序根据用户的操作将两个不同的视图控制器之一推送到导航控制器堆栈上。代码如下:

    - (void)mapViewDidFailLoadingMap:(MKMapView *)mapView withError:(NSError *)error
    {
        NSLog(@"mapViewDidFailLoadingMap: %@", [error localizedDescription]);
        [aiView stopAnimating];

        if (mapTypeInt == 0) {
            NSString *message = 
    [NSString stringWithFormat:@"Your signal is not currently 
     strong enough to download a map. Switching to table view."];
            UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Maps Unavailable"
                                                         message:message
                                                        delegate:nil
                                               cancelButtonTitle:@"OK"
                                               otherButtonTitles:nil];
            [av show];
            if (currentMode == @"retracingSteps")
            {
                RetraceViewController *rvc = 
    [[RetraceViewController alloc] initWithNibName:@"RetraceViewController" bundle:nil];
                [[self navigationController] pushViewController:rvc animated:YES];
            }
            else{
                TripTableViewController *ttvc = [[TripTableViewController alloc] init];
                [[self navigationController] pushViewController:ttvc animated:YES];
            }
        }
        else{
            [self setMapType:0];

            NSString *message = [NSString stringWithFormat:
    @"Your signal is not currently strong enough to download a satellite map. 
    Switching to Standard Map view."];
            UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Can't Use Satellite Maps"
                                                         message:message
                                                        delegate:nil
                                               cancelButtonTitle:@"OK"
                                               otherButtonTitles:nil];
            [av show];
        }



    }

昨天我在一个乡村山谷测试了较差的移动信号,并且正确的第二个视图控制器被推送到堆栈上。然后,当我锁定手机并在几分钟后重新检查应用程序时,我注意到的是,在醒来时,根视图控制器很快就会显示出来,然后是我预期的视图控制器。实际上,这样做是将第二个视图控制器的相同副本推送到堆栈上。当我不得不点击后退按钮六次才能返回根视图控制器时,我发现了这一点。

我希望应用程序在唤醒时执行的是立即显示手机锁定时处于活动状态的视图控制器,而不是根视图控制器。我不知道该怎么做。

4

2 回答 2

0

这可能是因为即使您锁定屏幕,您的应用程序仍会接收位置更新(位置服务可以在真实后台模式下运行代码),而且当您推送视图控制器时,堆栈中的前一个控制器仍然存在并且仍在接收位置更新和做所有事情都是设计好的,所以即使你推送另一个控制器,如果发生某些事情,实现的逻辑必须推送另一个控制器,这个视图控制器可以访问导航控制器,所以只需将另一个控制器添加到堆栈并推送它(对于堆栈中的所有 VC,导航控制器仍然相同)

因此,您要做的就是在您推送另一个控制器时停止处理这种情况,并在您弹回控制器时重新启动以执行此操作

于 2013-05-23T10:45:20.000 回答
0

这个错误是由处理程序 mapViewDidFailLoadingMap:withError: 在控制传递给推送的视图控制器之前被多次调用引起的。我通过对处理程序方法进行三处更改来修复了该错误:1)我删除了 UIAlertView 显示并将其移至推送的视图控制器;2)如果处理程序被第二次调用,我使用了一个标志(在 viewDidAppear 中初始化:)从处理程序返回而不执行推送;3)我在分配它之前检查了推送视图控制器的存在。

当所有这三个更改都进行时,视图控制器之间的转换正确发生。代码如下:

- (void)mapViewDidFailLoadingMap:(MKMapView *)mapView withError:(NSError *)error

{ [aiView 停止动画];

if (mapViewFailed > 0) {
    mapViewFailed ++;
    return;
}

if (mapTypeInt == 0) {
    [map setUserTrackingMode:MKUserTrackingModeNone];
    [self removeAsNoticationsObserver];

    if ([currentMode isEqualToString:@"retracingSteps"])
    {
        if (!rvc) {
            rvc = [[RetraceViewController alloc] initWithNibName:@"RetraceViewController" bundle:nil];
        }
        [[self navigationController] pushViewController:rvc animated:YES];
    }
    else{
        if (!ttvc) {
            ttvc = [[TripTableViewController alloc] init];
        }
        [[self navigationController] pushViewController:ttvc animated:YES];
    }
    mapViewFailed ++;


}
else{
    [self setMapType:0];

    NSString *message = [NSString stringWithFormat:@"Your signal is not currently strong enough to download a satellite map. Switching to Standard Map view."];
    UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Can't Use Satellite Maps"
                                                 message:message
                                                delegate:nil
                                       cancelButtonTitle:@"OK"
                                       otherButtonTitles:nil];
    [av show];
}

}

毫无疑问,作为 iOS 新手,有一个我还不知道的更优雅的解决方案。

于 2013-06-08T11:59:38.183 回答