2017-06-22 60 views
0

所以我有3个视图控制器,将其命名为:与定制类iOS的对象 - 视图初始化一次

- Dashboard 
- Map 
- Location List 

在MapVC,我附上1个大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

你为什么[self.mapStoreView removeFromSuperview] self.mapStoreView = nil;你是从插座做出的,并且你没有再次分配。所以它没有显示。请勿删除此 –

+0

,因为您在viewDidDisappear中重新映射。 – KKRocks

+0

如果它没有显示,为什么当我从仪表板到MapVC时加载? – Alvin

回答

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

当你推到一个新的viewController称为位置列表,您可以从超级视图中删除您mapStoreView,当你回来,你不收回mapStoreView,所以它走了。

如果自我是disappeard,包括navigationController推向一个新的视图控制器,流行,提出了一种新的ViewController并关闭自控制器viewDidDisappear将被调用。

如果你想释放内存,你应该做的dealloc方法,这些作业时控制器dealloced这将被调用。

这样的:

 -dealloc 
     { 
[self.mapStoreView removeFromSuperview]; 
     self.mapStoreView = nil; 
     self.mapStoreView.mkMapView.delegate = nil; 
     [self.mapStoreView.mkMapView removeAnnotations:self.mapStoreView.mkMapView.annotations]; 
     self.results = nil; 
    } 
+0

我使用你的dealloc函数后,内存不会减少先生,而是它不断增加(内存泄漏) – Alvin

+0

我没有确切的工作来释放内存,这只是一个例子。你应该删除viewDidDisappear中的代码解决你的问题,内存泄漏是另一个问题,你应该分析你的代码,找到保留周期并解决它,让你的控制器免费.Fix内存泄漏总是一个复杂的工作 – Jay

0

变化如下:

你做零mapStoreView的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]; 
} 

当您的视图 - 控制释放自动调用此方法。

-(void)dealloc 
      { 
      [self.mapStoreView removeFromSuperview]; 
      self.mapStoreView = nil; 
      self.mapStoreView.mkMapView.delegate = nil; 
      [self.mapStoreView.mkMapView removeAnnotations:self.mapStoreView.mkMapView.annotations]; 
      self.results = nil; 
     } 
+0

所以,我必须删除self.mapStoreView =在视图中的零将会消失? – Alvin

+0

是的。你需要删除所有mapStoreView的相关行 – KKRocks

+0

为什么你在viewDidDisappear中被删除? – KKRocks