由viewWillAppear没有被调用见控制器结构设计上的误区

由viewWillAppear没有被调用见控制器结构设计上的误区,第1张

概述问题   Hi, I'm stuck! I can't see why viewWillAppear doesn't run in my code but viewDidLoad runs. If I understand it correctly viewDidLoad runs once on the first instance and viewWillAppear runs every t

问题

 

Hi,

I'm stuck! I can't see why vIEwWillAppear doesn't run in my code but vIEwDIDLoad runs. If I understand it correctly vIEwDIDLoad runs once on the first instance and vIEwWillAppear runs every time a vIEw is added to the stack of vIEws to display.

I see others have had this issue but some how their solutions of calling vIEwWillAppear directly causes my app to crash. Other solutions were related to Navigation Controller and pushingVIEw's but thats not what i'm using either! What am I missing?

Thanks in advance for your help! :)

See below: VIEw Controller #1 - Currently being displayed on screen

 -(IBAction)somebuttonpressed:(ID)sender{
 
NSLog(@"FirstVIEwController - somebuttonpressed");
 
SecondVIEwController *secondVIEwController = [[SecondVIEwController alloc] initWithNibname:@"SecondVIEwController" bundle:nil];
  secondVIEwController
.myLocation = self.myLocation;
  secondVIEwController
.myDatabase = self.myDatabase;
 
[self.vIEw addSubvIEw:secondVIEwController.vIEw];
 
//[secondVIEwController vIEwWillAppear:YES];
}

       SecondVIEwController:

 - (voID)vIEwWillAppear:(BOol)animated {
 
NSLog(@"SecondVIEwController - vIEwWillAppear");
 
[super vIEwWillAppear:animated];
 
// updating ivars with data
}


// Implement vIEwDIDLoad to do additional setup after loading the vIEw,typically from a nib. 
 - (voID)vIEwDIDLoad {   
   
NSLog(@"SecondVIEwController - vIEwDIDLoad");
   
[super vIEwDIDLoad];
}
    

If I understand it correctly vIEwDIDLoad runs once on the first instance and vIEwWillAppear runs 

every time a vIEw is added to the stack of vIEws to display.

 


回答:

-vIEwDIDLoad is called every time a UIVIEwController's vIEw is loaded. That may be many times during

a single controller's life as the vIEw may be unloaded to free up memory when it is not visible and reloaded, 

triggering another call to -vIEwDIDLoad,when needed.

-vIEwWillAppear: is called when a UIVIEwController's vIEw becomes visible. However UIKit assumes that

UIVIEwController's vIEws will fill their window. nesting UIVIEwControllers' vIEws is an example of abusing 

UIViewControllers and will result in unexpected behavior. As you have seen.

See About Custom View Controllers in the VIEw Controller Programming GuIDe for iOS:

Each custom vIEw controller object you create is responsible for managing all of the vIEws in a single 

vIEw hIErarchy. In iPhone applications,the vIEws in a vIEw hIErarchy Traditionally cover the entire 

screen,but in iPad applications they may cover only a portion of the screen. The one-to-one correspondence

between a vIEw controller and the vIEws in its vIEw hIErarchy is the key design consIDeration. You should

not use multiple custom vIEw controllers to manage different portions of the same vIEw hIErarchy. Similarly,

you should not use a single custom vIEw controller object to manage multiple screens worth of content.

-vIEwDIDLoad-vIEwDIDLoad-vIEwWillAppear:
总结

以上是内存溢出为你收集整理的由viewWillAppear没有被调用控制器结构设计上的误区全部内容,希望文章能够帮你解决由viewWillAppear没有被调用见控制器结构设计上的误区所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1094036.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存