1

嗨,任何人都可以帮我解决以下问题:我有一个 ViewController,里面有一个 MKMapView,当我弹出这个视图时,一切都很好,但是当我尝试将这个 ViewController 推回我的 NavigationController 堆栈时,我的应用程序意外崩溃。我试图通过启用 NSZombie 来解决这个问题,它在从 xcode 运行时运行良好,但是当我尝试在模拟器(和我的 iPhone)中运行它而不使用 xcode 时,它​​仍然崩溃。我还尝试在 ViewWillDissapear、ViewDidDissapear 和 dealloc 中使我的 mapview 删除为零,但我仍然遇到同样的问题。谁能帮我这个?

这是应用程序崩溃时的日志输出:

-[__NSCFType observedViewControllerChange:]:无法识别的选择器发送到实例 0x79a6f50 2012-09-14 11:28:13.878* **[456:12503] *由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'-[__NSCFType observedViewControllerChange: ]: unrecognized selector sent to instance 0x79a6f50' * First throw call stack: (0x19af022 0x179ecd6 0x19b0cbd 0x1915ed0 0x1915cb2 0x12c3a29 0x197a855 0x197a778 0x120819a 0x9d9c97 0xb810be 0xb813c7 0xb81436 0x931e49 0x931f34 0x6c0b54 0x1f2c509 0x18e6803 0x18e5d84 0x18e5c9b 0x30967d8 0x309688a 0x901626 0x46ad 0x2d55) terminate called throwing an exception(lldb)

我正在使用弧。这是一些代码:我在 viewDidLoad 函数中做了很多事情,我认为问题可能出在这里,代码如下:

这是.h:

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/Corelocation.h>
#import "AddressViewController.h"
#import "Utilities.h"
#import "MarqueeLabel.h"
#import "CustomButton.h"



@interface LocationViewController : UIViewController<MKMapViewDelegate,CLLocationManagerDelegate,UIGestureRecognizerDelegate>{
    CLLocationManager *locationManager;
    MKPointAnnotation *centerAnnotation;
    int initialState;
    NSDictionary * json;
    bool google;
    bool isBackgroundRunning;
    double x;
    CLLocationCoordinate2D selectedCoordinate;
    MarqueeLabel *continuousLabel;
    __weak IBOutlet CustomButton *cont;
    UILongPressGestureRecognizer *lgr;
    UIPanGestureRecognizer *panGesture;
    UIPinchGestureRecognizer *pinchGesture;
    UISwipeGestureRecognizer *swipeGesture;
    __weak IBOutlet UIButton *myLoc;
    BOOL first;

}
@property (strong, nonatomic) IBOutlet MKMapView *myMapView;
- (IBAction)showMe:(id)sender;
@property (strong, nonatomic) MKPointAnnotation *centerAnnotation;

@end




- (void)viewDidLoad
{
    [super viewDidLoad];
    first = YES;
    initialState = YES;
    nrGooglePosts = 0;

    [self.myMapView setShowsUserLocation:YES ];


    myMapView.centerCoordinate = myMapView.userLocation.coordinate;
    locationManager = [[CLLocationManager alloc] init];
    [locationManager setDelegate:self];
    [locationManager setDistanceFilter:kCLDistanceFilterNone];
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];


    NSMutableArray *userL =[[NSUserDefaults standardUserDefaults] objectForKey:@"user"];
    if([userL count] == 0) NSLog(@"user nu e salvat");
    [[Utilities get] setName:[[userL valueForKey:@"name"] objectAtIndex:0]];
    [[Utilities get] setPhone:[[userL valueForKey:@"phone"] objectAtIndex:0]];
    [[Utilities get] setCode:[[userL valueForKey:@"code"] objectAtIndex:0]];
    [[Utilities get] setUserId:[[userL valueForKey:@"userid"] objectAtIndex:0]];


    centerAnnotation = [[MKPointAnnotation alloc] init];
    MKPointAnnotation * pc = [[MKPointAnnotation alloc] init];
    pc.coordinate = myMapView.centerCoordinate;
    pc.title = @"Map Center";
    pc.subtitle= [[NSString alloc] initWithFormat:@"%f, %f",pc.coordinate.latitude,pc.coordinate.longitude];    
    [self.myMapView addAnnotation:pc];
    centerAnnotation = pc;

    lgr= [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
    lgr.minimumPressDuration=0.1;
    lgr.allowableMovement = 5;
    lgr.delegate = self ;
    [myMapView addGestureRecognizer:lgr];

    panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
    panGesture.delegate = self;
    [myMapView addGestureRecognizer:panGesture];

    pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
    pinchGesture.delegate = self;
    [myMapView addGestureRecognizer:pinchGesture];

    swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
    swipeGesture.delegate = self;
    [myMapView addGestureRecognizer:swipeGesture];


    NSMutableArray * arr=[[NSUserDefaults standardUserDefaults ] objectForKey:@"locationList"];
    if([arr count]){
        for(int i = 0 ; i < [arr count] ; i++ ){
            NSMutableDictionary * adr = [[arr objectAtIndex:i] mutableCopy];
            if(adr){
                CLLocationDegrees lat = [[adr objectForKey:@"latitude"] doubleValue] ;
                CLLocationDegrees lon = [[adr objectForKey:@"longitude"] doubleValue] ;
                CustomMKAnnotation *ann = [[CustomMKAnnotation alloc] init ];
                CLLocationCoordinate2D coord = { lat, lon };

                ann.coordinate = coord;

                ann.title = [adr objectForKey:@"street"] ;   

                ann.subtitle = [[NSString alloc] initWithFormat:@"Nr:%@  Bl:%@  Sc:%@",[adr objectForKey:@"number"],[adr objectForKey:@"bloc"],[adr objectForKey:@"scara"]];

               MKAnnotationView *aux = [[MKAnnotationView alloc] initWithAnnotation:ann reuseIdentifier:nil];  
                UIImage *poza = [UIImage imageNamed:@"star.png"];
                UIImageView *pic = [[UIImageView alloc] initWithImage:poza];
                [aux addSubview:pic];
                aux.canShowCallout = YES;
                [myMapView addAnnotation:aux.annotation];


            }

        }
    }
    google = YES;
    json = [self getLocationFromGoogleWithCoordinates:myMapView.userLocation.coordinate];
    NSString *status = [[NSString alloc] initWithFormat:@"%@",[json objectForKey:@"status"]];
    if([status compare:@"ZERO_RESULTS"] == 0 && [status compare:@"OVER_QUERY_LIMIT"] == 0)
    {
        google=NO;
        NSLog(@"%@",status);
    }

    continuousLabel = [[MarqueeLabel alloc] initWithFrame:CGRectMake(32, 20, 191, 20) rate:50.0f andFadeLength:10.0f];
    continuousLabel.marqueeType = MLContinuous;
    continuousLabel.numberOfLines = 1;
    continuousLabel.opaque = NO;
    continuousLabel.enabled = YES;
    continuousLabel.shadowOffset = CGSizeMake(0.0, -1.0);
    continuousLabel.textAlignment = UITextAlignmentLeft;

    continuousLabel.backgroundColor = [UIColor clearColor];
    continuousLabel.font = [UIFont boldSystemFontOfSize:13];
    continuousLabel.text = [[Utilities get] streetName];
    [continuousLabel setBackgroundColor:[UIColor clearColor]];
    [continuousLabel setTextColor:[UIColor whiteColor]];
    continuousLabel.opaque = NO ;
    myLoc.opaque=NO;
    [myLoc setBackgroundColor:[UIColor clearColor]];
    [self.view addSubview:continuousLabel];


    [logoutButton setTitle:@"Log Out" forState:UIControlStateNormal];
    [logoutButton addTarget:self action:@selector(logout:) forControlEvents:UIControlEventTouchUpInside];


    UIBarButtonItem *logoutBarButton = [[UIBarButtonItem alloc] initWithCustomView:logoutButton]; 
    self.navigationItem.leftBarButtonItem = logoutBarButton;


    [self.navigationController.navigationBar setBackgroundColor:[UIColor clearColor]];



}
- (void) viewWillDisappear:(BOOL)animated
{

    [self.navigationController setNavigationBarHidden:YES animated:animated];

    [self stopTrackingLocation];

    [myMapView removeAnnotations:myMapView.annotations];
      self.myMapView.delegate = nil;

      self.view = nil;
    [super viewWillDisappear:animated];
}


- (void)stopTrackingLocation
{
    if (myMapView.showsUserLocation)
    {
        // Crashes here
        myMapView.showsUserLocation = NO;
    }
}
4

1 回答 1

1

如我所见,您正在使用ARC。

嘿伙计,不要这样做:

self.view = NULL. //REMOVE THIS!!!!

您不能再次推送 UIViewController,因为没有与之相关的 UIVIew。它会在 UINavigationController 中崩溃,因为实际上没有什么可以推送。

第二个问题,它不会导致崩溃但有一个地方。您正在删除地图视图委托。但是如果你要再次推送这个控制器,第二次地图视图将被禁用。

您应该将此功能移至 dealloc 方法。你应该在 viewWillAppear: 方法中显示用户位置,而不是在 viewDidLoad: 中,因为这个方法只会执行一次。当您第二次尝试推送 UIVIewController 时,它不会生效。所以请更改此功能:

- (void) viewWillAppear:(BOOL)animated{
     [self.myMapView setShowsUserLocation:YES ];
}

- (void) viewWillDisappear:(BOOL)animated
{

    [self.navigationController setNavigationBarHidden:YES animated:animated];

    [self stopTrackingLocation];

    [myMapView removeAnnotations:myMapView.annotations];

    [super viewWillDisappear:animated];
}

-(void) dealloc{
     self.myMapView.delegate = nil;
     //In ARC we don't call [super dealloc]
}
于 2012-09-14T08:41:52.250 回答