有什么办法可以避免像tabbarcontroller那样调用viewdidload方法吗?
Is there any way to avoid calling viewdidload method like tabbarcontroller?
我正在开发一个基于地图的应用程序。因此,一旦用户打开 MapViewController
,我就会每 5 秒加载一些数据。
我正在使用导航控制器(推送视图控制器)。
所以每次当用户去 MapViewController
viewdidload
方法调用。我不想那样。
这就是为什么我试图避免 viewdidload
方法,例如 tabbarcontroller
。
有什么办法可以实现吗?
viewDidLoad
被调用是因为当您将它从导航控制器的顶部弹出时,您的 MapViewController
正在被释放。当您重新创建视图控制器时,它会重新分配,并且视图会再次加载。如果您在包含导航控制器的 class 中保留对 MapViewController
的引用,则 ARC 不会释放该对象,您可以使用此引用将其推回堆栈,因此 viewDidLoad
不会再被调用。
编辑:添加参考代码:
MapViewContoller *mapViewController; // declared globally so there's a strong reference.
- (void) openMapViewController {
if (!mapViewController) {
mapViewController = [self.storyboard instantiateViewControllerWithIdentifier: MapViewControllerID];
}
[self.navigationController pushViewController: mapViewController, animated: YES];
}
viewDidLoad 旨在用于以下情况,不可能或无法高效地在 XIB 中配置 100% 的接口。 有时,您希望设置特定的 属性 XIB 中的视图不可用。有时,您使用自动布局,然后您意识到用于该功能的编辑器实际上比编写自动布局代码更糟糕。有时,您需要在将图像设置为按钮背景之前修改图像。
如果您不想做这些事情,请将您的 viewDidLoad 清空。不如避而远之。或者
Add code conditionaly into your viewDidLoad.
(void)viewDidLoad {
[super viewDidLoad];
if(condition) {
// put your code here
}
}
试试这个
-(void)clickForPush{
// declarre viewcontroller e.g 'saveRecipeVC' instance globally in interface
if (!saveRecipeVC) {
saveRecipeVC = [self.storyboard instantiateViewControllerWithIdentifier:SaveRecipeVCID];
}
[self.navigationController pushViewController:saveRecipeVC animated:YES];
}
我正在开发一个基于地图的应用程序。因此,一旦用户打开 MapViewController
,我就会每 5 秒加载一些数据。
我正在使用导航控制器(推送视图控制器)。
所以每次当用户去 MapViewController
viewdidload
方法调用。我不想那样。
这就是为什么我试图避免 viewdidload
方法,例如 tabbarcontroller
。
有什么办法可以实现吗?
viewDidLoad
被调用是因为当您将它从导航控制器的顶部弹出时,您的 MapViewController
正在被释放。当您重新创建视图控制器时,它会重新分配,并且视图会再次加载。如果您在包含导航控制器的 class 中保留对 MapViewController
的引用,则 ARC 不会释放该对象,您可以使用此引用将其推回堆栈,因此 viewDidLoad
不会再被调用。
编辑:添加参考代码:
MapViewContoller *mapViewController; // declared globally so there's a strong reference.
- (void) openMapViewController {
if (!mapViewController) {
mapViewController = [self.storyboard instantiateViewControllerWithIdentifier: MapViewControllerID];
}
[self.navigationController pushViewController: mapViewController, animated: YES];
}
viewDidLoad 旨在用于以下情况,不可能或无法高效地在 XIB 中配置 100% 的接口。 有时,您希望设置特定的 属性 XIB 中的视图不可用。有时,您使用自动布局,然后您意识到用于该功能的编辑器实际上比编写自动布局代码更糟糕。有时,您需要在将图像设置为按钮背景之前修改图像。
如果您不想做这些事情,请将您的 viewDidLoad 清空。不如避而远之。或者
Add code conditionaly into your viewDidLoad.
(void)viewDidLoad {
[super viewDidLoad];
if(condition) {
// put your code here
}
}
试试这个
-(void)clickForPush{
// declarre viewcontroller e.g 'saveRecipeVC' instance globally in interface
if (!saveRecipeVC) {
saveRecipeVC = [self.storyboard instantiateViewControllerWithIdentifier:SaveRecipeVCID];
}
[self.navigationController pushViewController:saveRecipeVC animated:YES];
}