所以我有3个视图控制器,命名为:
- Dashboard
- Map
- Location List
在MapVC中,我附加了一个大的uiview和自定义类的XMapView。在XMapView内部我有这段代码
- (id)initWithFrame:(CGRect)aRect {
if ((self = [super initWithFrame:aRect])) {
[self commonInit];
}
return self;
}
- (id)initWithCoder:(NSCoder*)coder {
if ((self = [super initWithCoder:coder])) {
[self commonInit];
}
return self;
}
- (void)commonInit{
UIView *mapView = [[[NSBundle mainBundle] loadNibNamed:@"XMapView"
owner:self
options:nil] objectAtIndex:0];
mapView.frame = self.bounds;
mapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self addSubview: mapView];
}
所以基本上MapVC只是注入XmapView并显示它。
我在MapVC控制器中有这个代码。请注意,我已将MapVC内的自定义类视图发布到.h文件。
//.h file
@property (strong, nonatomic) IBOutlet XMapView *mapStoreView;
//.m file
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
// this following code will load the Map from the XMapView to the MapVC
[self.mapStoreView loadMap:self.results viewController:self selectedOutletFromList:self.selectedOutletFromList];
}
- (void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
[self.mapStoreView removeFromSuperview];
self.mapStoreView = nil;
self.mapStoreView.mkMapView.delegate = nil;
[self.mapStoreView.mkMapView removeAnnotations:self.mapStoreView.mkMapView.annotations];
self.results = nil;
}
序列是仪表板 - > MapVC - >位置清单
测试用例:
1. Dashboard -> MapVC, map will load.
2. MapVC -> Location List -> MapVC, map will not load.
3. Dashboard -> MapVC -> Dashboard -> MapVC, map will always load.
这里有什么问题?我错过了什么吗?
答案 0 :(得分:0)
在您的代码中
- (void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
[self.mapStoreView removeFromSuperview];
self.mapStoreView = nil;
self.mapStoreView.mkMapView.delegate = nil;
[self.mapStoreView.mkMapView removeAnnotations:self.mapStoreView.mkMapView.annotations];
self.results = nil;
}
当您推送到名为Location List的新viewController时,您从超级视图中删除了mapStoreView,当您返回时,您没有恢复mapStoreView,所以它已经消失。
如果self消失,将调用viewDidDisappear,包括推送一个新的viewcontroller,从navigationController弹出,提供一个新的viewcontroller并关闭自我控制器。如果你想释放内存,你应该在dealloc方法中执行这些工作,这将在取消分配控制器时调用。
像这样: -dealloc
{
[self.mapStoreView removeFromSuperview];
self.mapStoreView = nil;
self.mapStoreView.mkMapView.delegate = nil;
[self.mapStoreView.mkMapView removeAnnotations:self.mapStoreView.mkMapView.annotations];
self.results = nil;
}
答案 1 :(得分:0)
更改如下:
你是mapStoreView的nil IBOutlet所以当你来自位置时它不会加载
它将HomeVc加载到MapVc,因为你正在创建mapVc的新实例,以便在那里创建新的iboutlet。
您的测试用例
1. Dashboard -> MapVC, map will load. - because new instance of MapVC created on Dashboard
2. MapVC -> Location List -> MapVC, map will not load. - because you are nil iboutlet in ViewDidDisappear so it is not longer for load.
3. Dashboard -> MapVC -> Dashboard -> MapVC, map will always load. - because new instance of MapVC created on Dashboard always.
进行如下更改
- (void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
}
当您的viewcontroller取消分配时,此方法会自动调用。
-(void)dealloc
{
[self.mapStoreView removeFromSuperview];
self.mapStoreView = nil;
self.mapStoreView.mkMapView.delegate = nil;
[self.mapStoreView.mkMapView removeAnnotations:self.mapStoreView.mkMapView.annotations];
self.results = nil;
}