2011-06-03 6 views
1

Pourriez-vous me donner des exemples de création de l'instance d'instance NSBox pour la placer dans le NSView?Créer un exemple d'exécution NSBox

+0

J'ai ajouté un exemple qui fonctionne avec la récupération de place activée. Faites-moi savoir si vous voulez que je le répare afin que, lorsqu'il est compilé sans récupération de place, il ne fuit pas. –

Répondre

2
NSView *superview = …; // Reference to the view that will contain the box 
         // for instance, [window contentView] 

NSUInteger resizeAllMask = (NSViewWidthSizable 
    | NSViewHeightSizable 
    | NSViewMinXMargin 
    | NSViewMaxXMargin 
    | NSViewMinYMargin 
    | NSViewMaxYMargin); 

// This is the box. We use an autoresizing mask so that it occupies 
// the entire superview 
NSBox *box = [[NSBox alloc] initWithFrame:[superview bounds]]; 
[box setAutoresizingMask:resizeAllMask]; 
[box setBoxType:NSBoxPrimary]; 
[box setBorderType:NSBezelBorder]; 
[box setTitle:@"This is a box"]; 

// This is the box' content view. It represents the box contents. 
// By default, a box autoresizes its content view so that it occupies 
// the entire box 
NSView *boxContentView = [[NSView alloc] initWithFrame:NSZeroRect]; 
[box setContentView:boxContentView]; 

// For example, we add a text field to the box' content view 
NSRect textRect = {{0,0}, {100,20}}; 
NSTextField *textField = [[NSTextField alloc] initWithFrame:textRect]; 
[textField setStringValue:@"Hey there"]; 
[boxContentView addSubview:textField]; 

[superview addSubview:box]; 
+0

J'ai compilé le code sans prise en charge de la récupération de place et il n'y a aucune fuite pour tous les objets alloués sont en cours de libération: \t [case removeFromSuperview]; \t [libération de boîte]; \t [libération de textField]; \t [version boxContentView]; Merci beaucoup! –

Questions connexes