由viewWillAppear没有被调用见控制器结构设计上的误区
来源:互联网 发布:局域网通话软件 编辑:程序博客网 时间:2024/05/16 15:37
问题
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.
- 由viewWillAppear没有被调用见控制器结构设计上的误区
- ios的视图控制器的viewWillAppear和viewDidAppear什么时候被调用
- ios的视图控制器的viewWillAppear和viewDidAppear什么时候被调用
- iphone上有时候viewWillAppear不被调用
- xamarin iOS viewWillAppear 没有调用问题
- UINavigationController中viewWillAppear不被调用的原因?
- UINavigationController下的viewcontroller不调用viewwillappear
- 关于-viewWillAppear:等无法调用的总结
- [zz] UINavigationController的addSubView不会调用viewWillAppear
- UINavigationController下的viewcontroller不调用viewwillappear
- 关于-viewWillAppear:等无法调用的总结
- viewWillAppear:等无法调用的总结
- viewWillAppear:等无法调用的总结
- viewWillDisappear,viewWillAppear不调用的解决方法
- viewWillDisappear,viewWillAppear不调用的解决方法
- viewWillDisappear,viewWillAppear不调用的解决方法
- viewWillDisappear,viewWillAppear不调用的解决方法
- iOS: viewWillAppear和viewDidAppear什么时候被调用
- linux typedef的四个用途和两个陷阱
- Reg Certificate to IE
- jmx是什么?
- 中断解析
- 太恐怖了!什么都能查!!
- 由viewWillAppear没有被调用见控制器结构设计上的误区
- Redhat常用操作记录笔记
- 自写易飞ERP上线方案:环境设定
- linux下传输文件
- 金山词霸 & IE9取词兼容性补丁包下载(来自官方)
- 中断系统简介
- 一生做好一件事
- 开机时无法进入系统,提示windows system32/winload.exe 无法加载
- Excel操作类备份