viewController备注

1、按结构可以对iOS的所有ViewController分成两类:

1)、主要用于展示内容的ViewController,这种ViewController主要用于为用户展示内容,并与用户交互,如UITableViewController,UIViewController。

2)、用于控制和显示其他ViewController的ViewController。这种ViewController一般都是一个 ViewController的容器。如UINavigationController,UITabbarController。它们都有一个属 性:viewControllers。其中UINavigationController表示一种Stack式结构,push一个 ViewController或pop一次,因此后一个ViewController一般会依赖前一个ViewController。而 UITabbarController表示一个Array结构,各个ViewController是并列的。

第一种ViewController会经常被继承,用来显示不同的数据给用户。而第二种很少被继承,除非你真的需要自定义它。

2、当view被添加其他view中之前时,会调用viewWillAppear,而之后会调用viewDidAppear。

当view从其他view中移出之前时,会调用viewWillDisAppear,而之后会调用viewDidDisappear。

当view不在使用,而且是disappeared,受到内存警告时,那么viewController会将view释放并将其指向nil。

3、由于Controller加载View时,会自动将一些View对象指向其对应的IBOutlet变量。

所以当view被卸载时我们必须在viewDidUnload将这些变量release掉,ViewController不会自动做这件事。

具体做法是将变量设置为空,(注意和dealloc中将变量release的区别)注意此时Controller的view属性是空的。

4.popViewControllerAnimated被调用的时候,前一个viewController页面会调用viewWillAppear,而不会调用viewDidload.因为其已经存在于导航栈中:

You don't need to instantiate your first controller in order to pop to it. It already exists.

viewDidLoad will only run when you load the viewController for the first time (i.e. when you push to it). When you push to other controllers they are put onto a stack (imagine a stack of cards). When you push another card onto the stack the cards beneath it are already there.

When you pop it is like removing a card from the stack. But the card underneath is already there so it doesn't need to load again. All it does is run viewWillAppear.

All you need to do to pop back is...

[self.navigationController popViewControllerAnimated:YES];

That's it.

Remove the stuff about NewSongController (if that is what you are trying to go back to).

Then in the NewSongController function - (void)viewWillAppear:animated; put the code you want to run when you get back to it.

Hope that helps

原文地址:https://www.cnblogs.com/baozou/p/3418048.html