将保留对象分配给弱属性;对象将在分配后释放 [英] Assigning retained object to weak property; object will be released after assignment

查看:94
本文介绍了将保留对象分配给弱属性;对象将在分配后释放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了一些源代码:

 KGModalContainerView *containerView = 
     self.containerView = 
         [[KGModalContainerView alloc] initWithFrame:containerViewRect];

它给了我:将保留对象分配给弱属性;对象将在作业后发布

编辑:

@interface KGModal()
  @property (strong, nonatomic) UIWindow *window;
  @property (weak, nonatomic) KGModalViewController *viewController;
  @property (weak, nonatomic) KGModalContainerView *containerView;
  @property (weak, nonatomic) UIView *contentView;
@end

KGModalContainerView *containerView = 
    self.containerView = 
        [[KGModalContainerView alloc] initWithFrame:containerViewRect];
containerView.modalBackgroundColor = self.modalBackgroundColor;
containerView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin |
                                 UIViewAutoresizingFlexibleRightMargin |
                                 UIViewAutoresizingFlexibleTopMargin |
                                 UIViewAutoresizingFlexibleBottomMargin;
containerView.layer.rasterizationScale = [[UIScreen mainScreen] scale];
contentView.frame = (CGRect){padding, padding, contentView.bounds.size};
[containerView addSubview:contentView];
[viewController.view addSubview:containerView];


推荐答案

我想你的 containerView 属性声明为属性。如果您希望某个属性具有属性,则应该已经保留了该属性。这是一个例子:

I suppose your containerView property is declared with weak attribute. If you want to have a weak attribute to a property someone should have already retained it. Here's an example:

@property (nonatomic, weak) KGModalContainerView *containerView;
...
-(void)viewDidLoad {
    [super viewDidLoad];
    KGModalContainerView *myContainerView = [[KGModalContainerView alloc] initWithFrame:containerViewRect]; // This is a strong reference to that view
    [self.view addSubview:myContainerView]; //Here self.view retains myContainerView
    self.containerView = myContainerView; // Now self.containerView has weak reference to that view, but if your self.view removes this view, self.containerView will automatically go to nil.

 // In the end ARC will release myContainerView, but it's retained by self.view and weak referenced by self.containerView
}

这篇关于将保留对象分配给弱属性;对象将在分配后释放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆