`
flycomos.lee
  • 浏览: 276322 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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

    博客分类:
  • ios
阅读更多

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)。上述的做法也是为了符合这一准则。

分享到:
评论

相关推荐

    ios-封装设置安全区域代码.zip

    [self.view addSubview:view]; view.backgroundColor = [UIColor yellowColor]; //设置contentview,四边都在安全区域 [self am_makeSafeView:view]; UIButton *butt new]; button.backgroundColor = [UIColor...

    ios-DCHChangeStateButton.zip

    自定义一个菜单按钮,通过切换按钮状态,对列表数据进行排序,直接调用即可 for (int i = 0; i < titleArr.count; i ) { UIView *view = [[UIView alloc] initWithFrame:CGRectMake(i*width, 64, width, 40)];...

    iOS 各种动画

    [self.view addSubview:imageView]; //Twitter style splash SKSplashIcon *twitterSplashIcon = [[SKSplashIcon alloc] initWithImage:[UIImage imageNamed:@"twitterIcon.png"] animationType:...

    IOS点赞效果按钮源代码下载

    #import "CatZanButton" ...[self.view addSubview:zanBtn]; [zanBtn setClickHandler:^(CatZanButton *zanButton) { if (zanButton.isZan) { //Do something }else{ //Do something } }];

    ios-一个简单的类似于系统UIAlerView.zip

    无聊的时候写的一个类似于系统的alertview,有个小小的动画,只有取消和确定键,代码只供参考,不建议项目使用 使用方法 XCJAlert *popView = [[XCJAlert alloc]initWithFrame:self.view.bounds withTitle:@"我是...

    ios-Clock for iOS (秒针两种旋转样式).zip

    一个简单的 clock 框架 用法简介 ClockView *clockView = [[ClockView alloc] initWithFrame:CGRectMake(0, 200, 200, 200)]; [self.view addSubview:clockView]; [clockView start]; ........ [clockView...

    IOS7下可AddSubView的AlertView

    在解决关于IOS7下Alert不能再AddsubView的问题时候,弄到的解决方法。用法基本和原来的UIAlertView一样 dismiss 用 close实现了

    ios-点击切换状态の按钮Multi-Button.zip

    [self.view addSubview:btn]; ② MultiButton *btn = [[MultiButton alloc]initWithFrame:CGRectMake(0, 0, 200, 30)]; btn.center = CGPointMake(self.view.center.x, 300); [btn setName:@"volume" state:...

    ios-JDLayout-超简易的约束布局.zip

    [self.view1 addSubview:label1]; UILabel *label2 = [[UILabel alloc] init]; label2.text = @"我是垂直平分2"; label2.backgroundColor = [UIColor blueColor]; label2.textAlignment = NSTextAlignmentCenter; ...

    长按拖动排序

    // [self.view addSubview:stBtn]; // stBtn.buttonArray = [[NSMutableArray alloc]init]; // for (NSInteger i = 0; i ; i++) { // UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(i*375/4.0, 0, 375...

    ios-闪烁的文字,多种闪烁效果.zip

    [self.view addSubview:label1]; WSShiningLabel *label2 = [[WSShiningLabel alloc] init]; label2.frame = CGRectMake(50, 105, 200, 25); label2.text = @"变成要你保护的她"; label2.textColor = ...

    ios-仿照微信、微博的弹出action sheet写的一个工具类.zip

    仿照微信、微博的弹出action sheet写的一个工具类 github地址:https://github.com/ashen-zhao/ASSheet 使用方法: AS_Sheet *a = [[AS_Sheet alloc] initWithFrame:self.view.bounds titleArr:@[@"从手机相册...

    ios-图片浏览器.zip

    [weakSelf.collectionView addSubview:showView]; showView.backgroundColor = [UIColor blackColor]; //消失回调 [showView setDismissBlock:^{ self.tabBarController.tabBar.hidden = NO; self....

    ios-获取验证码按钮.zip

    一个获取验证码,实现倒计时的封装类。 有OC版本和Swift版本,git地址: https://github.com/ZHShare/XFetchCodeButton.git // 部分代码 Swift: let codeButton = XFetchCodeButton(frame: CGRect(x: 20, y: ...

    ios-CRBoxInputView.zip

    该组件可以在短信验证码,密码输入框,或者手机号码输入框这些场景中使用。...[self.view addSubview:boxInputView]; 其他用法请查看github主页:https://github.com/CRAnimation/CRBoxInputView

    IOS 代码 自动化布局

    [self.view addSubview:bgImage]; NSDictionary *viewsbg = NSDictionaryOfVariableBindings(self.view,bgImage); [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-0-[bgImage...

    ios开发小技巧

    //创建一个基于位图的图形上下文并指定大小为CGSizeMake(200,400) UIGraphicsBeginImageContext(CGSizeMake(200,400)); //renderInContext 呈现接受者及其子范围到指定的上下文 [self.view.layer renderInContext:...

    ios-iOS自定义视频播放器(avPlayer简易版).zip

    JWAVPlayer 自定义简单的支持横屏竖屏切换的Avplayer ```javascript JWPlayer*player=[[JWPlayer ... [self.view addSubview:player]; ``` [git地址](https://github.com/JarvisHot/JWAVPlayer) 给个star呗

    BKZoomView:一个将放大其父视图的UIView

    一个UIView,它将放大到其父视图。 它仅需三行代码即可实现,并且非常易于使用。 可以选择使用可自定义的缩放比例来拖动它。 ##如何 在视图中的任何位置初始化缩放视图。 BKZoomView *zoomView = [[BKZoomView ...

    归档操作,源码

    归档操作,源码,详细标注,值得拥有,, CustomImgView *customView = [[CustomImgView alloc]initWithFrame:CGRectMake(50, 100, 200, 200)]; customView.backgroundColor =... [self.view addSubview:customView];

Global site tag (gtag.js) - Google Analytics