In Interface Builder, c'è un modo per impostare "identificatore" di un NSView. In questo caso, userò l'identificatore "54321" come stringa identificatore.
NSView Conforme allo NSUserInterfaceItemIdentification Protocol, che è un identificatore univoco come NSString. Si può camminare nella gerarchia della vista e trovare NSView con quell'identificatore.
Quindi, per costruire su questo post su come ottenere l'elenco dei NSViews, Get ALL views and subview of NSWindow, si potrebbe quindi trovare il NSView con l'identificatore che si desidera:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSView *viewToFind = [self viewWithIdentifier:@"54321"];
}
- (NSView *)viewWithIdentifier:(NSString *)identifier
{
NSArray *subviews = [self allSubviewsInView:self.window.contentView];
for (NSView *view in subviews) {
if ([view.identifier isEqualToString:identifier]) {
return view;
}
}
return nil;
}
- (NSMutableArray *)allSubviewsInView:(NSView *)parentView {
NSMutableArray *allSubviews = [[NSMutableArray alloc] initWithObjects: nil];
NSMutableArray *currentSubviews = [[NSMutableArray alloc] initWithObjects: parentView, nil];
NSMutableArray *newSubviews = [[NSMutableArray alloc] initWithObjects: parentView, nil];
while (newSubviews.count) {
[newSubviews removeAllObjects];
for (NSView *view in currentSubviews) {
for (NSView *subview in view.subviews) [newSubviews addObject:subview];
}
[currentSubviews removeAllObjects];
[currentSubviews addObjectsFromArray:newSubviews];
[allSubviews addObjectsFromArray:newSubviews];
}
for (NSView *view in allSubviews) {
NSLog(@"View: %@, tag: %ld, identifier: %@", view, view.tag, view.identifier);
}
return allSubviews;
}
Oppure, dal momento che si sta utilizzando una sottoclasse NSView, è potrebbe impostare il "tag" di ogni vista in fase di esecuzione. (Oppure, è possibile impostare l'identificatore in fase di esecuzione.) La cosa bella del tag, è che esiste una funzione predefinita per trovare una vista con un tag specifico.
// set the tag
NSInteger tagValue = 12345;
[self.myButton setTag:tagValue];
// find it
NSButton *myButton = [self.window.contentView viewWithTag:12345];
fonte
2013-04-25 21:37:05
Sembra fantastico, grazie per il suggerimento! Ci proverò. Ahimè, niente di pre-definito nella vaniglia Cocoa NSView che può essere usato ma immagino che sto ancora pensando troppo in un modo PowerPlant/MFC ;-) – Jay
La risposta qui sotto funziona per una vista generica. Basta impostare Identifer in xib e accedervi a livello di codice con view.identifier – Colin
Qualche idea su quale logica dietro il tag sia in sola lettura in NSView? – Mercurial