didMoveToSuperView 引發的思考


 

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

 

[objc]  view plain copy
 
  1. </pre><p></p></blockquote><p></p><p class="p1">實現方法</p><p class="p1"></p><pre name="code" class="objc">-(void)didMoveToSuperview{  
  2.     NSLog(@"test didMoveToSuperview");  
  3. }  

接下來我們在ViewController.m中添加view

 

 

 

[objc]  view plain copy
 
  1. #import "ViewController.h"  
  2. #import "MyTestView.h"  
  3.   
  4. @interface ViewController ()  
  5.   
  6. @end  
  7.   
  8. @implementation ViewController  
  9.   
  10. - (void)viewDidLoad {  
  11.     [super viewDidLoad];  
  12.     // Do any additional setup after loading the view, typically from a nib.  
  13.       
  14.     MyTestView *testView=[[MyTestView alloc]initWithFrame:CGRectMake(10, 120, 300, 300)];  
  15.     testView.backgroundColor=[UIColor grayColor];  
  16.     [self.view addSubview:testView];  
  17.       
  18. }  
  19.   
  20. - (void)didReceiveMemoryWarning {  
  21.     [super didReceiveMemoryWarning];  
  22.     // Dispose of any resources that can be recreated.  
  23. }  
  24.   
  25. @end  


 

運行 是不是看到 log了

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM