1.
- (void)didMoveToSuperview 通知視圖已經移動到一個新的父視圖中
2.
/**系統自動調用(留給子類去實現)**/
- (void)didAddSubview:(UIView *)subview;
- (void)willRemoveSubview:(UIView *)subview;
- (void)willMoveToSuperview:(UIView *)newSuperview;
- (void)didMoveToSuperview; <<< ----------------
- (void)willMoveToWindow:(UIWindow *)newWindow;
- (void)didMoveToWindow;
/**系統自動調用**/
3.
iOS UIView 類目 UIViewHierarchy
有一個方法
didMoveToSuperview
官方解釋如下
Tells the view that its superview changed.
大致意思: 當view的父級視圖更改的時候會調用此方法
The default implementation of this method does nothing. Subclasses can override it to perform additional actions whenever the superview changes.
大致意思:此方法默認實現,不做任何操作。子視圖可以實現此方法,添加自己所需要的功能
通過以上我們可以理解為 次方法在view被添加新的父級視圖的時候會調用。
我們嘗試測試一下
首先新建一個項目TestDidMoveToSuperview
新建一個View 繼承UIView 起名:MyTestView
- </pre><p></p></blockquote><p></p><p class="p1">實現方法</p><p class="p1"></p><pre name="code" class="objc">-(void)didMoveToSuperview{
- NSLog(@"test didMoveToSuperview");
- }
接下來我們在ViewController.m中添加view
- #import "ViewController.h"
- #import "MyTestView.h"
- @interface ViewController ()
- @end
- @implementation ViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view, typically from a nib.
- MyTestView *testView=[[MyTestView alloc]initWithFrame:CGRectMake(10, 120, 300, 300)];
- testView.backgroundColor=[UIColor grayColor];
- [self.view addSubview:testView];
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- @end
運行 是不是看到 log了