为什么我的UIViewController与UITableView在从使用MKMapView的UITableView快速切换后崩溃?

时间:2015-06-21 09:23:24

标签: ios objective-c uitableview mkmapview

在我的iOS应用程序的一个部分中,有一个视图允许用户从总线列表中进行选择。当用户选择总线时,它将它们带到显示地图上的公交路线的不同视图控制器。除非由于某种原因用户选择总线然后非常快速地按下路线中的后退按钮,所以一切都很完美,在这种情况下它有时会崩溃应用程序(并非总是如此)。发生崩溃时,总线选择视图是可见的,因此它会进入该视图。它崩溃了。当用户非常快速地按下路径视图中的后退按钮时,会出现此问题。否则没有问题。后退按钮作为模态连接到总线选择视图控制器。

MapsViewController.h(这是用户从公交车列表中选择的地方)

#import <UIKit/UIKit.h>

@interface MapsViewController : UIViewController
{
    NSArray *weekdayBusesNumbers;
    NSArray *saturdayBusesNumbers;
    NSArray *sundayBusesNumbers;

    NSArray *weekdayBusesNames;
    NSArray *saturdayBusesNames;
    NSArray *sundayBusesNames;
}

@property (strong, nonatomic) IBOutlet UILabel *mapsTitleLabel;
@property (strong, nonatomic) IBOutlet UILabel *selectABusLabel;
@property (strong, nonatomic) IBOutlet UITableView *tableView;

@end

MapsViewController.m(这是用户从公交车列表中选择的地方)

#import "MapsViewController.h"
#import "GlobalVariables.h"
#import "RouteMapViewController.h"

@interface MapsViewController ()

@end

@implementation MapsViewController

- (void)viewDidLoad
{
    [super viewDidLoad];


    self.mapsTitleLabel.center = CGPointMake(self.view.center.x, 33.5);
    self.selectABusLabel.frame = CGRectMake(0, 49, self.view.frame.size.width, 38);
    self.tableView.frame = CGRectMake(-15, 87, self.view.frame.size.width+15, self.view.frame.size.height-87);
    [self.tableView setContentOffset:CGPointMake(0, savedRouteTableViewSelectionHeightOffset) animated:NO];


    weekdayBusesNumbers = @[@"002", @"006"];
    weekdayBusesNames = @[@"Bus 002", @"Bus 006"];

    saturdayBusesNumbers = @[@"101", @"103"];
    saturdayBusesNames = @[@"Bus 101", @"Bus 103"];

    sundayBusesNumbers = @[@"300", @"303"];
    sundayBusesNames = @[@"Bus 300", @"Bus 303"];
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(section == 0) // Weekday
        return weekdayBusesNumbers.count;

    else if(section == 1) // Saturday
        return saturdayBusesNumbers.count;

    else if(section == 2) // Sunday
        return sundayBusesNumbers.count;

    return 0;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if(cell == nil)
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    else
        [[cell.contentView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];


    UILabel *numberLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 0, 60, 40)];
    numberLabel.font = [UIFont boldSystemFontOfSize:30];
    numberLabel.adjustsFontSizeToFitWidth = true;
    numberLabel.textAlignment = NSTextAlignmentCenter;


    UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, self.tableView.frame.size.width-numberLabel.frame.size.width-55, 40)];
    nameLabel.font = [UIFont boldSystemFontOfSize:20];
    nameLabel.adjustsFontSizeToFitWidth = true;
    nameLabel.textAlignment = NSTextAlignmentCenter;


    if(indexPath.section == 0) // Weekday
    {
        numberLabel.text = weekdayBusesNumbers[indexPath.row];
        nameLabel.text = weekdayBusesNames[indexPath.row];
    }

    else if(indexPath.section == 1) // Saturday
    {
        numberLabel.text = saturdayBusesNumbers[indexPath.row];
        nameLabel.text = saturdayBusesNames[indexPath.row];
    }

    else if(indexPath.section == 2) // Sunday
    {
        numberLabel.text = sundayBusesNumbers[indexPath.row];
        nameLabel.text = sundayBusesNames[indexPath.row];
    }


    [cell.contentView addSubview:numberLabel];
    [cell.contentView addSubview:nameLabel];


    return cell;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0)
        currentMapViewing = [NSString stringWithFormat:@"Weekday%@", weekdayBusesNumbers[indexPath.row]];

    else if(indexPath.section == 1)
        currentMapViewing = [NSString stringWithFormat:@"Saturday%@", saturdayBusesNumbers[indexPath.row]];

    else if(indexPath.section == 2)
        currentMapViewing = [NSString stringWithFormat:@"Sunday%@", sundayBusesNumbers[indexPath.row]];


    savedRouteTableViewSelectionHeightOffset = self.tableView.contentOffset.y;


    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
    RouteMapViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"RouteMapView"];
    [self presentViewController:vc animated:NO completion:nil];
}



- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}



- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{

    if(section == 0)
        return @"        Monday to Friday";

    else if(section == 1)
        return @"        Saturday";

    else if(section == 2)
        return @"        Sunday";

    return @"";
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}



- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
    if ([view isKindOfClass: [UITableViewHeaderFooterView class]]) {

        UITableViewHeaderFooterView* castView = (UITableViewHeaderFooterView*) view;
        castView.contentView.backgroundColor = [UIColor lightGrayColor];
        [castView.textLabel setTextColor:[UIColor blackColor]];
    }
}

@end

RouteMapViewController.h(这是公共汽车的路线在地图上显示的地方)

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>

@interface RouteMapViewController : UIViewController<MKMapViewDelegate>
{
    MKPolyline *routeLine;
    MKPolylineView* routeLineView;
}

@property (strong, nonatomic) IBOutlet UIButton *backButton;
@property (strong, nonatomic) IBOutlet UILabel *busNumberTopLabel;
@property (strong, nonatomic) IBOutlet UILabel *busNameLabel;
@property (strong, nonatomic) IBOutlet UILabel *leftOfBusNameLabel;
@property (strong, nonatomic) IBOutlet UILabel *rightOfBusNameLabel;
@property (strong, nonatomic) IBOutlet MKMapView *mapView;
@property (strong, nonatomic) IBOutlet UISegmentedControl *mapTypeSegmentedControl;

@end

RouteMapViewController.m(这是公共汽车的路线在地图上显示的地方)

#import "RouteMapViewController.h"
#import "GlobalVariables.h"
#import "CustomAnnotationView.h"
@import CoreLocation;

@interface RouteMapViewController ()

@end

@implementation RouteMapViewController

- (void)viewDidLoad
{
    [super viewDidLoad];


    self.busNumberTopLabel.center = CGPointMake(self.view.center.x, 33.5);
    self.busNameLabel.frame = CGRectMake(20, 49, self.view.frame.size.width-40, 38);
    self.busNameLabel.adjustsFontSizeToFitWidth = true;
    self.leftOfBusNameLabel.frame = CGRectMake(0, 49, 20, 38);
    self.rightOfBusNameLabel.frame = CGRectMake(self.view.frame.size.width-20, 49, 20, 38);
    self.mapView.frame = CGRectMake(0, 87, self.view.frame.size.width, self.view.frame.size.height-87);
    self.mapTypeSegmentedControl.frame = CGRectMake(self.mapTypeSegmentedControl.frame.origin.x, self.view.frame.size.height-36, 304, 29);


    MKCoordinateRegion region = {{0.0, 0.0}, {0.0, 0.0}};

    if([currentMapViewing isEqualToString:@"Weekday002"])
    {
        self.busNumberTopLabel.text = @"002";
        self.busNameLabel.text = @"Bus 002";
        region.center.latitude = 52.500802;
        region.center.longitude = -86.965345;
        region.span.latitudeDelta = 0.074971;
        region.span.longitudeDelta = 0.088683;
    }

    else if([currentMapViewing isEqualToString:@"Weekday006"])
    {
        self.busNumberTopLabel.text = @"006";
        self.busNameLabel.text = @"Bus 006";
        region.center.latitude = 53.487891;
        region.center.longitude = -87.004044;
        region.span.latitudeDelta = 0.033501;
        region.span.longitudeDelta = 0.039619;
    }

    else if([currentMapViewing isEqualToString:@"Saturday101"])
    {
        self.busNumberTopLabel.text = @"101";
        self.busNameLabel.text = @"Bus 101";
        region.center.latitude = 52.502169;
        region.center.longitude = -85.966747;
        region.span.latitudeDelta = 0.079915;
        region.span.longitudeDelta = 0.094534;
    }

    else if([currentMapViewing isEqualToString:@"Saturday103"])
    {
        self.busNumberTopLabel.text = @"103";
        self.busNameLabel.text = @"Bus 103";
        region.center.latitude = 53.487662;
        region.center.longitude = -86.004759;
        region.span.latitudeDelta = 0.029682;
        region.span.longitudeDelta = 0.035102;
    }

    else if([currentMapViewing isEqualToString:@"Sunday300"])
    {
        self.busNumberTopLabel.text = @"300";
        self.busNameLabel.text = @"Bus 300";
        region.center.latitude = 51.469874;
        region.center.longitude = -87.010039;
        region.span.latitudeDelta = 0.094139;
        region.span.longitudeDelta = 0.111295;
    }

    else if([currentMapViewing isEqualToString:@"Sunday303"])
    {
        self.busNumberTopLabel.text = @"303";
        self.busNameLabel.text = @"Bus 303";
        region.center.latitude = 52.481819;
        region.center.longitude = -85.030556;
        region.span.latitudeDelta = 0.088188;
        region.span.longitudeDelta = 0.104282;
    }

    [self getAndSetInitialMap];
    [self.mapView setRegion:region];
}



- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];

    [self.mapView removeFromSuperview];
    self.mapView.delegate = nil;
    self.mapView = nil;
}



- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    if([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    NSString *annotationIdentifier = @"CustomViewAnnotation";
    CustomAnnotationView *customAnnotationView = (CustomAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier];

    if(!customAnnotationView)
    {
        customAnnotationView = [[CustomAnnotationView alloc] initWithAnnotationImage:annotationIdentifier reuseIdentifier:annotationIdentifier annotationViewImage:[UIImage imageNamed:@"greenStopPin.png"]];

        customAnnotationView.canShowCallout = YES;
    }

    return customAnnotationView;
}



- (void)getAndSetInitialMap
{
    switch(currentMapMode)
    {
        case 0:
            self.mapView.mapType = MKMapTypeStandard;
            break;

        case 1:
            self.mapView.mapType = MKMapTypeSatellite;
            break;

        case 2:
            self.mapView.mapType = MKMapTypeHybrid;
            break;

        default:
            break;
    }

    [self.mapTypeSegmentedControl setSelectedSegmentIndex:currentMapMode];
}



- (IBAction)setMap:(id)sender
{
    switch(((UISegmentedControl *)sender).selectedSegmentIndex)
    {
        case 0:
            self.mapView.mapType = MKMapTypeStandard;
            currentMapMode = 0;
            break;

        case 1:
            self.mapView.mapType = MKMapTypeSatellite;
            currentMapMode = 1;
            break;

        case 2:
            self.mapView.mapType = MKMapTypeHybrid;
            currentMapMode = 2;
            break;

        default:
            break;
    }
}



- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
    MKOverlayView *overlayView = nil;

    if(overlay == routeLine)
    {
        routeLineView = [[MKPolylineView alloc] initWithPolyline:routeLine];
        routeLineView.strokeColor = [UIColor blueColor];
        routeLineView.fillColor = [UIColor blueColor];
        routeLineView.lineWidth = 3;

        overlayView = routeLineView;
    }
    return overlayView;
}



- (void)mapViewDidFinishRenderingMap:(MKMapView *)mapView fullyRendered:(BOOL)fullyRendered
{
    NSString* path = [[NSBundle mainBundle] pathForResource:currentMapViewing ofType:@"txt"];
    NSString* fileContents = [NSString stringWithContentsOfFile:path
                                                       encoding:NSUTF8StringEncoding
                                                          error:NULL];


    NSCharacterSet *newlineCharSet = [NSCharacterSet newlineCharacterSet];
    NSArray *fileLines = [fileContents componentsSeparatedByCharactersInSet:newlineCharSet];
    NSMutableArray *routeLatitudes = [[NSMutableArray alloc] init];
    NSMutableArray *routeLongitudes = [[NSMutableArray alloc] init];


    for(int i=0; i<fileLines.count; i++)
    {
        MKPointAnnotation *pointToAdd = [stopInformationDictionary objectForKey:[NSString stringWithFormat:@"%@", fileLines[i]]];

        [self.mapView addAnnotation:pointToAdd];

        if(i==0)
            [self.mapView selectAnnotation:pointToAdd animated:YES];

        [routeLatitudes addObject:[NSNumber numberWithDouble:pointToAdd.coordinate.latitude]];
        [routeLongitudes addObject:[NSNumber numberWithDouble:pointToAdd.coordinate.longitude]];
    }


    MKMapPoint* pointsArray = malloc(sizeof(CLLocationCoordinate2D) * [routeLatitudes count]);
    for(int i = 0; i < [routeLatitudes count]; i++)
    {
        CLLocationCoordinate2D workingCoordinate;
        workingCoordinate.latitude=[[routeLatitudes objectAtIndex:i] doubleValue];
        workingCoordinate.longitude=[[routeLongitudes objectAtIndex:i] doubleValue];
        MKMapPoint point = MKMapPointForCoordinate(workingCoordinate);
        pointsArray[i] = point;
    }

    routeLine = [MKPolyline polylineWithPoints:pointsArray count:[routeLatitudes count]];
    [self.mapView addOverlay:routeLine];
}

@end

崩溃

Thread 1 Queue : com.apple.main-thread (serial)
#0  0x37482f66 in objc_msgSend ()
#1  0x2cfe917a in __125-[UIPopoverController _presentPopoverFromRect:embeddedInView:usingViewForLayoutConstraints:permittedArrowDirections:animate:]_block_invoke416 ()
#2  0x2ca54940 in -[UIViewAnimationBlockDelegate _didEndBlockAnimation:finished:context:] ()
#3  0x2ca54558 in -[UIViewAnimationState sendDelegateAnimationDidStop:finished:] ()
#4  0x2ca5446e in -[UIViewAnimationState animationDidStop:finished:] ()
#5  0x2c443046 in CA::Layer::run_animation_callbacks(void*) ()
#6  0x0023f186 in _dispatch_client_callout ()
#7  0x00242e9c in _dispatch_main_queue_callback_4CF ()
#8  0x293ab888 in __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ ()
#9  0x293a9fa8 in __CFRunLoopRun ()
#10 0x292f59a0 in CFRunLoopRunSpecific ()
#11 0x292f57b2 in CFRunLoopRunInMode ()
#12 0x30ace1a8 in GSEventRunModal ()
#13 0x2ca80694 in UIApplicationMain ()
#14 0x00108d04 in main at HIDDEN…… 

0 个答案:

没有答案