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)。上述的做法也是为了符合这一准则。
分享到:
相关推荐
[self.view addSubview:view]; view.backgroundColor = [UIColor yellowColor]; //设置contentview,四边都在安全区域 [self am_makeSafeView:view]; UIButton *butt new]; button.backgroundColor = [UIColor...
自定义一个菜单按钮,通过切换按钮状态,对列表数据进行排序,直接调用即可 for (int i = 0; i < titleArr.count; i ) { UIView *view = [[UIView alloc] initWithFrame:CGRectMake(i*width, 64, width, 40)];...
[self.view addSubview:imageView]; //Twitter style splash SKSplashIcon *twitterSplashIcon = [[SKSplashIcon alloc] initWithImage:[UIImage imageNamed:@"twitterIcon.png"] animationType:...
#import "CatZanButton" ...[self.view addSubview:zanBtn]; [zanBtn setClickHandler:^(CatZanButton *zanButton) { if (zanButton.isZan) { //Do something }else{ //Do something } }];
无聊的时候写的一个类似于系统的alertview,有个小小的动画,只有取消和确定键,代码只供参考,不建议项目使用 使用方法 XCJAlert *popView = [[XCJAlert alloc]initWithFrame:self.view.bounds withTitle:@"我是...
一个简单的 clock 框架 用法简介 ClockView *clockView = [[ClockView alloc] initWithFrame:CGRectMake(0, 200, 200, 200)]; [self.view addSubview:clockView]; [clockView start]; ........ [clockView...
在解决关于IOS7下Alert不能再AddsubView的问题时候,弄到的解决方法。用法基本和原来的UIAlertView一样 dismiss 用 close实现了
[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:...
[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...
[self.view addSubview:label1]; WSShiningLabel *label2 = [[WSShiningLabel alloc] init]; label2.frame = CGRectMake(50, 105, 200, 25); label2.text = @"变成要你保护的她"; label2.textColor = ...
仿照微信、微博的弹出action sheet写的一个工具类 github地址:https://github.com/ashen-zhao/ASSheet 使用方法: AS_Sheet *a = [[AS_Sheet alloc] initWithFrame:self.view.bounds titleArr:@[@"从手机相册...
[weakSelf.collectionView addSubview:showView]; showView.backgroundColor = [UIColor blackColor]; //消失回调 [showView setDismissBlock:^{ self.tabBarController.tabBar.hidden = NO; self....
一个获取验证码,实现倒计时的封装类。 有OC版本和Swift版本,git地址: https://github.com/ZHShare/XFetchCodeButton.git // 部分代码 Swift: let codeButton = XFetchCodeButton(frame: CGRect(x: 20, y: ...
该组件可以在短信验证码,密码输入框,或者手机号码输入框这些场景中使用。...[self.view addSubview:boxInputView]; 其他用法请查看github主页:https://github.com/CRAnimation/CRBoxInputView
[self.view addSubview:bgImage]; NSDictionary *viewsbg = NSDictionaryOfVariableBindings(self.view,bgImage); [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-0-[bgImage...
//创建一个基于位图的图形上下文并指定大小为CGSizeMake(200,400) UIGraphicsBeginImageContext(CGSizeMake(200,400)); //renderInContext 呈现接受者及其子范围到指定的上下文 [self.view.layer renderInContext:...
JWAVPlayer 自定义简单的支持横屏竖屏切换的Avplayer ```javascript JWPlayer*player=[[JWPlayer ... [self.view addSubview:player]; ``` [git地址](https://github.com/JarvisHot/JWAVPlayer) 给个star呗
一个UIView,它将放大到其父视图。 它仅需三行代码即可实现,并且非常易于使用。 可以选择使用可自定义的缩放比例来拖动它。 ##如何 在视图中的任何位置初始化缩放视图。 BKZoomView *zoomView = [[BKZoomView ...
归档操作,源码,详细标注,值得拥有,, CustomImgView *customView = [[CustomImgView alloc]initWithFrame:CGRectMake(50, 100, 200, 200)]; customView.backgroundColor =... [self.view addSubview:customView];