I have a UIView that has many instances and each one of them has a UIRecognizer.
When on of them is tapped I want to remove all the recognizers of the others.
<I have two ideas:
1/ Create a class array with all the instances static NSArray* instances;
, register them when initializing, unregister when deallocating. The array should have only weak references, otherwise they will never be deallocated.
2/ NSNotification. All instances can wait for a notification and if you tap, you send the notification.
If you just need to find all instances for debugging purposes, you can use the Allocations
instrument and change the Recorded Types
to only your class. This will give you a dandy list of all your objects. You can then interact with them using lldb
by using their address.
If they're all subviews of the same view, you could iterate over parentView.subviews
and find them that way. Something like this:
for (UIView *v in parentView.subviews) {
if ([v isKindOfClass:[MyViewClass class]]) {
// remove recognizer here
}
}
Another, more efficient, option would be to have a flag in your view controller that you set when the first recognizer is triggered and use to short-circuit any future recognizer handler calls. Something like this:
@property (nonatomic) BOOL shouldRespondToEvent;
@synthesize shouldRespondToEvent=_shouldRespondToEvent;
- (void)viewDidLoad {
[super viewDidLoad];
self.shouldRespondToEvent = YES;
// other viewDidLoad stuff here
}
- (void)gestureHandler:(UIGestureRecognizer*)recognizer {
if (!self.shouldRespondToEvent)
return;
self.shouldRespondToEvent = NO;
// rest of handler code here
}
First I'd say whatever you're trying to accomplish can likely be better accomplished with NSNotificationCenter, that said, if you still need to do this, the following will work and will be ARC compliant.
In your .h add this:
+(NSArray *)allInstances;
Then, add this to the bottom of your class's .m:
-(id)init {
//Note, I suppose you may need to account for exotic init types if you are creating instances of your class in non-traditional ways. I see in the docs that initWithType: exists for example, not sure what storyboard calls
self = [super init];
[[self class] trackInstance:self];
return self;
}
-(void)dealloc {
[[self class] untrackInstance:self];
}
static NSMutableArray *allWeakInstances;
+(void)trackInstance:(id)instance {
if (!allWeakInstances) {
allWeakInstances = [NSMutableArray new];
}
NSValue *weakInstance = [NSValue valueWithNonretainedObject:instance];
[allWeakInstances addObject:weakInstance];
}
+(void)untrackInstance:(id)instance {
NSValue *weakInstance = [NSValue valueWithNonretainedObject:instance];
[allWeakInstances removeObject:weakInstance];
}
+(NSArray *)allInstances {
NSMutableArray *allInstances = [NSMutableArray new];
for (NSValue *weakInstance in allWeakInstances) {
[allInstances addObject:[weakInstance nonretainedObjectValue]];
}
return allInstances.copy;
}
Then when you need all instances of the class, just call [Class allInstances];