簡體   English   中英

iOS-使用MKMapView彈出並推送視圖控制器時發生意外崩潰

[英]iOS - Unexpected crash when popping and pushing a view controller with a MKMapView

嗨,有人可以幫我解決以下問題:我有一個帶MKMapView的ViewController,當我彈出該視圖時,一切都很好,但是當我嘗試將此ViewController推回到我的NavigationController堆棧中時,我的應用程序意外崩潰。 我試圖通過啟用NSZombie來解決此問題,並且在從xcode運行時可以正常工作,但是當我嘗試在模擬器中(以及在我的iPhone上)不使用xcode運行它時,它仍然崩潰。 我也試圖在ViewWillDissapear,ViewDidDissapear和dealloc中使mapview delegete nil無效,但是我仍然遇到相同的問題。 誰能幫我這個?

這是應用程序崩潰時的日志輸出:

-[__ NSCFType觀察到的視圖控制器更改:]:無法識別的選擇器發送到實例0x79a6f50 2012-09-14 11:28:13.878 * ** [456:12503] *由於未捕獲的異常'NSInvalidArgumentException'終止了應用程序,原因:'-[__ NSCFType觀察到的視圖控制器更改: ]:無法識別的選擇發送到實例0x79a6f50' *第一擲調用堆棧:(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)終止叫做拋出異常(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;
    }
}

如我所見,您正在使用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]
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM