addSubview:

Adds a view to the end of the receiver’s list of subviews.

译:增加一个视图到接收者的子视图列表中。

- (void)addSubview:(UIView *)view

Parameters

view

The view to be added. This view is retained by the receiver. After being added, this view appears on top of any other subviews.

译:view参数代表被增加的view,这个view会被它的接收者retain一次(即引用计数+1)。增加完成之后,这个view将出现在接收者的其他子视图的上面。

ps:关于子视图的出现的层次的问题,可以从这些子视图被保存的数据结构来探寻答案
,每个视图都有个数组的属性,subviews,这个就是保存的它的子视图的引用。而这个数组的顺序就是代表了各个子视图被加入时的顺序。index=0
的就是最先被加入进去的,以此类推。所以,索引值越高的视图越不容易被覆盖。

Discussion

This method retains view and sets its next responder to the receiver, which is its new superview.

译:这个方法会retain一次view,并且设置它的下一个响应者是receiver,即它的新的父视图。

ps:在removeFromSuperview里已经说过,其实视图直接的操作往往牵涉到两个方面的操作,一个是视图的数据结构,一个是响应者链。当然,addsubview也不例外。

Views can have only one superview. If view already has a
superview and that view is not the receiver, this method removes the
previous superview before making the receiver its new superview.

译:每一个视图只能有唯一的一个父视图。如果当前操作视图已经有另外的一个父视图,则addsubview的操作会把它先从上一个父视图中移除(包括响应者链),再加到新的父视图上面。

Availability

  • Available in iOS 2.0 and later.

See Also

    • – insertSubview:atIndex:
    • – insertSubview:aboveSubview:
    • – insertSubview:belowSubview:
    • – exchangeSubviewAtIndex:withSubviewAtIndex:

addSubview和insertSubview的区别

addSubview 是将view加到所有层的最顶层

相当于将insertSubview的atIndex参数设置成view.subviews count

[view addSubview:oneview] == [view insertSubview:oneview atIndex:view.subviews count]

addSubview是加到最后
insertSubview是加到指定的位置

 

为什么要在addsubview:一个view对象后,release它?

先看代码:

1
2
3
IMGView *imgView = [[IMGView alloc] initWithFrame:CGRectMake(10, 0, 300, 300)];
[self.view addSubview:imgView];
[imgView release];

为什么imgView要release呢?可能很多人跟我一样,之前不是很清楚。 我们逐行分析一下

第一行,alloc一次,imgView对象retainCount为1,

二行,addSubview一次,此方法会把你传过去的对象retain一次,那么此时它的retainCount为2。self.view变为它的第二
个待有者。参考:The receiver retains view. If you use removeFromSuperview to
remove view from the view hierarchy, view is released.
第三行,调用release方法,此处释放对imgView的所有权,retainCount减1。

到语言句尾imgView的所有者只剩下self.view,并且它的retainCount仅为1。内存管理第一铁则,谁retain(alloc,copy)谁release(autorelease)。上述的做法也是为了符合这一准则。

addSubView需要注意的几个点的更多相关文章

  1. 关于addSubView需要注意的事项 -今天吃了一个大亏

    addSubview: Adds a view to the end of the receiver’s list of subviews. 译:增加一个视图到接收者的子视图列表中. - (void) ...

  2. addsubview跟insertsubview的区别

    子视图是以栈的方式存放的. 每次addsubview时都是在最后面添加. 每次在addsubview前和addsubview后可以看看[self.view.subViews count]: 你看看你的 ...

  3. pushViewController addSubview presentModalViewController视图切换

    1.pushViewController和popViewController来进行视图切换,首先要确保根视图是NavigationController,不然是不可以用的, pushViewContro ...

  4. addChildViewController 与 addSubview

    在viewcontrollerA中, 如果想把controllerB.view添加进来, 可以用 addSubview, 但如果controllerB中有个事件, 使用到 self.navigatio ...

  5. iOS: 神奇的addSubView

    看着addSubView, 本以为是添加多个对象, 但通过测试代码, 发现同一个对象在addSubView中只会添加一次. 想想, 视图对象是通过引用得到的. 在视图的子视图集中, 只保存一个相应的对 ...

  6. 在[self addsubView:xxx]中,self.name 和 _name的区别

    在[self addsubView:xxx]中,self.name 和 _name的区别self.name 会调用重写的getter方法,而_name添加的只是_name 这个成员变量

  7. 【编程技巧】addSubview和insertSubview的区别

    addSubview 是将view加到所有层的最顶层 相当于将insertSubview的atIndex参数设置成view.subviews count 即 [view addSubview:onev ...

  8. 如何处理UIVIew addsubview 不显示subview

    老代码: addsubview不显示uilabel -(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSIn ...

  9. [self.view addSubview:vc2.view]程序崩溃的解决办法

    - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. UIButt ...

随机推荐

  1. centos6.4 网络适配器设置仅主机模式

    网络适配器设置仅主机模式时: 1.vmnet1网卡必须开启

  2. cookies的理解

    设置cookie 每个cookie都是一个名/值对,可以把下面这样一个字符串赋值给document.cookie: document.cookie="userId=828"; 如果 ...

  3. Xilium.CefGlue怎么使用Js调用C#方法

    第1篇:.NET多种WebKit内核/Blink内核浏览器初步测评报告http://www.wuleba.com/?p=23590 第2篇:Xilium CefGlue 关于 CLR Object 与 ...

  4. mac安装IE浏览器

    1.首先得下载一个WineBottler for mac. 2.下载完毕之后,打开dmg文件后将WineBottler Combo里面的Wine和WineBottler这两个程序拖拉进应用程序. 3. ...

  5. Keepalived+MySQL实现高可用(转)

    http://www.cnblogs.com/wingsless/p/4033093.html   MHA高可用 http://www.cnblogs.com/gomysql/p/3856484.ht ...

  6. 详解@Autowired、@Qualifier和@Required

    A.@Autowired org.springframework.beans.factory.annotation.Autowired public @interface Autowired Mark ...

  7. 【python】环境变量的配置

    在windows下安装python之后,系统并不会自动添加相应的环境变量.此时不能在命令行直接使用python命令. 1.首先需要在系统中注册python环境变量:假设python的安装路径为c:\p ...

  8. LintCode "Backpack"

    A simple variation to 0-1 Knapsack. class Solution { public: /** * @param m: An integer m denotes th ...

  9. webform 页面传值的方法总结

    ASP.NET页面之间传递值的几种方式   页面传值是学习asp.net初期都会面临的一个问题,总的来说有页面传值.存储对象传值.ajax.类.model.表单等.但是一般来说,常用的较简单有Quer ...

  10. POM (Project Object Model)简介

    1  概念介绍 一个项目所有的配置都放置在 POM 文件中:定义项目的类型.名字,管理依赖关系,定制插件的行为等等.比如说,你可以配置 compiler 插件让它使用 java1.5 来编译. < ...