没有调用类的setter

时间:2014-03-26 05:27:30

标签: ios objective-c class selector setter

我正在尝试实施car2go风格的应用程序,您可以在其中单击注释视图,然后可以预订汽车。问题是当我尝试segue我想要传递汽车对象时,我检查对象是否执行选择器设置车辆,但if语句失败,虽然我很确定它已实现。有人可以告诉我为什么吗?这是一个学校项目。

#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];
            }
        }
    }
}

}

这是它正在进行的视图控制器。

.h文件

#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文件

#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

1 个答案:

答案 0 :(得分:1)

很可能segue.destinationViewController不是CompanyVehicleViewController的实例。

在故事板中验证视图控制器的类型是否设置为正确的类。