目标:尝试在我的ViewController中重用UIView用于不同的xib文件。一个xib有一个MKMapView IBOutlet,另一个xib有一个cameraView(不包含在下面的代码中)。
问题:在没有任何IBOutlets的情况下交换XIB文件工作正常但是当我在其中一个xib文件中添加了一个用于MKMapView的IBOutlet时,它会抛出此错误:
"原因:' [setValue:forUndefinedKey:]:此类与键映射不符合键值编码"
xib文件:
MapView.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@interface MapView : UIView <MKMapViewDelegate>
@property (weak, nonatomic) IBOutlet MKMapView *map;
@end
MapView.m
#import "MapView.h"
#import <MapKit/MapKit.h>
@implementation MapView
-(id) initWithCoder:(NSCoder *)aDecoder
{
if (self = [super initWithCoder:aDecoder])
{
[self load];
}
return self;
}
-(id)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame])
{
[self load];
}
return self;
}
-(void)load
{
UIView *view = [[[NSBundle bundleForClass:[self class]] loadNibNamed:@"MapView" owner:self options:nil] firstObject];
[self addSubview:view];
view.frame = self.bounds;
}
@end
的ViewController:
DashboardViewController:
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@interface DasboardViewController : UIViewController <MKMapViewDelegate>
@end
DashboardViewController.m:
#import "DasboardViewController.h"
#import "MapView.h"
@interface DasboardViewController ()
@property (strong, nonatomic) IBOutlet UIView *viewMain;
@property(nonatomic, strong) MapView *mapNib;
@end
@implementation DasboardViewController
- (void)viewDidLoad {
[super viewDidLoad];
_mapNib = [[NSBundle mainBundle] loadNibNamed:@"MapView" owner:self options:nil].firstObject;
[_mapNib setFrame:_viewMain.bounds];
[_mapNib setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
[_viewMain addSubview:_mapNib];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end