ios didMoveToSuperview

来源:互联网 发布:如何看待辛亥革命 知乎 编辑:程序博客网 时间:2024/06/05 07:22

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 plaincopy
  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 plaincopy
  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(10120300300)];  
  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了


0 0
原创粉丝点击