Question

I am trying to implement a car2go style app where you click on an annotation view and then can reserve the car. The problem is when I try to segue I want to pass the car object over, I check if the object performs the selector set vehicle but the if statement fails although I am pretty sure it is implemented. Can someone tell me why? This is for a school project.

#import "MapViewController.h"
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#import "Vehicles+Company.h"  
#import "Vehicles+MKAnnotation.h"

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

if ([segue.identifier isEqualToString:@"reserveCar"]) {
    if ([sender isKindOfClass:[MKAnnotationView class]]) {
        MKAnnotationView *aView = sender;
        if ([aView.annotation isKindOfClass:[Vehicles class]]) {
            Vehicles *vehicle = aView.annotation;
            if ([segue.destinationViewController respondsToSelector:@selector(setVehicle:)]) {
                [segue.destinationViewController performSelector:@selector(setVehicle:) withObject:vehicle];
            }
        }
    }
}

}

This is the viewcontroller it is segueing to.

.h file

#import <UIKit/UIKit.h>
#import "Vehicles+MKAnnotation.h"
#import "Vehicles.h"
#import "Vehicles+Company.h"


@interface CompanyVehicleViewController : UIViewController

@property (nonatomic,strong) Vehicles *vehicle;

@end

.m file

#import "CompanyVehicleViewController.h"

@interface CompanyVehicleViewController ()

@end

@implementation CompanyVehicleViewController

-(void) setVehicle:(Vehicles *)vehicle{
_vehicle = vehicle;
self.title = vehicle.name;
NSLog(@"Vehicle is %@", vehicle);
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
 }

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
 }

@end
Was it helpful?

Solution

Most likely segue.destinationViewController isn't an instance of CompanyVehicleViewController.

Verify in your storyboard that the view controller's type is set to the proper class.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top