0
votes

I've created a UIView subclass with a UIButton inside with target on tap inside. The selector is implemented inside the UIView subclass and linked by Interface Builder.

But when I run addSubview in parent view controller tapping button will cause

performSelector:withObject:withObject:]: message sent to deallocated instance

(I use ARC) I added some console output in dealloc method and I see that right after viewDidLoad ends with addSubview of the UIView subclass, the sublass is deallocated.

How to prevent that sort of situation? I tried @property(nonatomic, retain), adding instance to some global array... But no luck.

I understand that ARC releses the object as there is no strong reference left but I couldn't force to prevent that situation.

Any help would be appreciated.

3

3 Answers

0
votes

Creating a strong reference of your subview class in your View Controller class may help you solve the issue.

@property (strong , nonatomic) UIView *subclassName;
0
votes

Are you storing reference of button (UIButton) you are trying to add as subview?

0
votes

The problem was in linking NIB to Class.

I linked File owner to class instead of linking the UIView object.

Normally when there is working on nib with UIViewController, there is a need to link File Owner to UIViewController class. But when working on UIView subclass the UIView object must be linked.