0

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.

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)

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.

Here is my awakeFromNib method:

-(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] < 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<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<[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<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];
}

Any help on this one will be greatly appreciated!

Steve

4

1 回答 1

0

在 NSView 的文档中-setSubviews:

此方法将受影响的视图和窗口区域标记为需要显示。

如果您不-setNeedsDisplay再次调用会发生什么whiteBox

于 2011-03-01T02:25:20.683 回答