We are subclassing UITabBarController as well as UITabBarControllerDelegate to handle certain events concerning tab switches.
Now in our custom tab bar controller we have:
- (id)initCustomTabBarController {
self = [super init];
if(self) {
[self setDelegate:[[CustomTabBarControllerDelegate alloc] init]];
// ...
}
return self;
}
Since we transitioned the project to ARC, the delegate is released to early which causes a tab switch run into a deallocated instance.
The property is defined as assign in UITabBarController.h – which I obviously have no influence on.
What can I do to make the delegate object “live” longer than for the init method?
The way you have done it, it is expected that the delegate will not outlive the object, because it is weak. Remember, you created the object, it’s up to you to hold on to it.
However – the pattern that you are using is incorrect.
The point of a delegate, is that it provides method implementations to a class that a class can’t add for itself, because it doesn’t have enough information. For example, a table view delegate. A table view, in order to be generic, cannot know how many rows or sections to display, so it asks it’s delegate to supply this information.
In your case, you have an object that is creating it’s own delegate. In which case, why bother having a delegate at all? Just implement the methods in the class.