14
4

2 回答 2

30

我想你的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
}
于 2013-02-27T10:48:05.237 回答
3

我作为 Objective C 初学者的 2 美分:

给出警告的行的右侧,

[[KGModalContainerView alloc] initWithFrame:containerViewRect]

在堆中创建一个对象,此时该对象没有被任何指针引用。然后将此对象分配给self.containerView。因为self.myContainerView是弱的,所以赋值不会增加右侧创建的对象的引用计数。所以当赋值完成后,对象的引用计数还是0,因此ARC会立即释放对象。

于 2015-02-06T00:53:59.060 回答