In an iPhone App I’ve got a view with a button on it. The Button is connected to an IBAction of MyClass.
@interface MyClass : NSObject
-(IBAction)buttonPressed:(id)sender;
@end
As soon as I click this button, I get an EXC_BAD_ACCESS. The class MyClass was added to the view by using the “Object” component from the right sidebar.
I think this is because my custom controller gets deallocated by ARC. Is this correct? Are objects which are added via the interface controller not automatically retained? How to retain the instance which I have connected via interface builder?
MyClassmust be a subclass ofUIView, notNSObject.If you think an object is being deallocated prematurely, implement the
-deallocmethod and add some logging and a breakpoint.If your custom view is on screen then its superview will hold a reference to that view and it will not be deallocated. If your custom view controller has a parent view controller such as a UINavigationController then its parent view controller will hold a reference to your view controller and it will not be deallocated.
Set a breakpoint in your
-buttonPressed:method to confirm that it’s being called correctly. Then step through the code.Look at the call stack at the time of the EXC_BAD_ACCESS to better isolate where the problem is. Is your -buttonPressed: method actually being called?