Note that there are some explanatory texts on larger screens.

plurals
  1. POSubviews disappearing on completion of awakeFromNib
    primarykey
    data
    text
    <p>I have a view with a NSBox subview (with CA enabled for shadow) and I'm trying to add my NSView subclass to the box dynamically during awakeFromNib.</p> <p>There will be a varying number of these NSViews depending on the user so I've made some code that determines how many rows and columns to make to arrange them nicely in the box. (I already considered a NSMatrix but it doesn't layout things the way I intend to)</p> <p>The problem is that after the awakeFromNib method finishes, the subviews suddenly disappear! I have put plenty of NSLog statements to try and track it down and all I'm finding out is that the subviews are clearly being added to the NSBox before they disappear. When running the app the subviews appear just briefly then disappear when the animation has finished.</p> <p>Here is my awakeFromNib method:</p> <pre><code>-(void)awakeFromNib { //Fill an array with buttons for each company object buttons = [[NSMutableArray alloc] init]; for (BCCompany *c in [[[BCParser sharedParser] managedObjectContext] fetchObjectsForEntityName:@"BCCompany" withPredicate:nil andSortDescriptor:nil]) { CompanyButton *button = [[CompanyButton alloc] initWithFrame:NSMakeRect(0, 0, 150, 150)]; [button setCompanyName:[c name]]; [buttons addObject:button]; } //Determine how many columns we can fit across the window -- 100px padding on each side -- 150px width for each button int noOfColumns = ([[self view] frame].size.width - 200) / 150; if ([buttons count] &lt; noOfColumns) noOfColumns = [buttons count]; //Determine how many rows will be needed int noOfRows = ([buttons count] + noOfColumns - 1) / noOfColumns; //Resize and reposition the box [whiteBox setFrameSize:NSMakeSize((noOfColumns * 150) + 60, (noOfRows * 150) + 20)]; [whiteBox setFrameOrigin:NSMakePoint(NSMidX([[self view] frame]) - ([whiteBox frame].size.width/2), NSMidY([[self view] frame]) - ([whiteBox frame].size.height/2) - 100)]; //Now iterate through each row and add items, checking if it's the last row subviewsToAdd = [[NSMutableArray alloc] init]; for (int i=0; i&lt;noOfRows; i++) { NSRect boxRect = [whiteBox frame]; NSArray *tempArray = [[NSArray alloc] init]; if (i == (noOfRows-1)) { //Final row tempArray = [buttons subarrayWithRange:NSMakeRange(i*noOfColumns, [buttons count])]; for (int j=0; j&lt;[tempArray count]; j++) { //Iterate through the remaining buttons and add them NSPoint buttonOrigin = NSMakePoint(5 + (j * 150), (boxRect.size.height) - (150 * (i+1))); NSRect buttonRect = NSMakeRect(buttonOrigin.x, buttonOrigin.y, 150, 150); [[tempArray objectAtIndex:j] setFrame:buttonRect]; NSLog(@"%@ Frame -- X:%f Y:%f -- j:%i", [[tempArray objectAtIndex:j] companyName], [[tempArray objectAtIndex:j] frame].origin.x, [[tempArray objectAtIndex:j] frame].origin.y, j); [subviewsToAdd addObject:[tempArray objectAtIndex:j]]; } } else { //Not the final row tempArray = [buttons subarrayWithRange:NSMakeRange(i*noOfColumns, noOfColumns)]; for (int j=0; j&lt;noOfColumns; j++) { //Position each one on this row NSPoint buttonOrigin = NSMakePoint(5 + (j * 150), (boxRect.size.height) - (150 * (i+1)) - 10); NSRect buttonRect = NSMakeRect(buttonOrigin.x, buttonOrigin.y, 150, 150); [[tempArray objectAtIndex:j] setFrame:buttonRect]; [subviewsToAdd addObject:[tempArray objectAtIndex:j]]; } } } [whiteBox setSubviews:subviewsToAdd]; [whiteBox setNeedsDisplay:YES]; } </code></pre> <p>Any help on this one will be greatly appreciated!</p> <p>Steve</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload