iphone - 将保留的对象分配给弱属性;对象将在分配后释放

标签 iphone ios

我使用了一些源代码:

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

它给了我:Assigning retained object to weak property;赋值后对象将被释放

编辑:

@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 属性是用 weak 属性声明的。如果您希望某个属性具有 weak 属性,应该有人已经保留了它。这是一个例子:

@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
}

关于iphone - 将保留的对象分配给弱属性;对象将在分配后释放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15110017/

相关文章:

iphone - 动画 UIPickerView 在横向模式下不旋转

iphone - 如何在 iPhone 中创建短信应用程序

iphone - DatePicker iPhone 只获取日期

ios - 如何计算我的动态 textView 的正确高度

iphone - 是否可以只对部分国家/地区实现应用内购买?

iphone - 如何从 Xcode 获取 ipa 文件以在真实设备中运行 iPhone 应用程序?

ios - 如何在谷歌地图上画线

iphone - 从不兼容的类型警告分配给

iphone - iOS - 如何将 html 资源文件的全部内容读入 NSString *?

ios - Stripe 和 Parse Cloud Code 向信用卡收费