Objective c awakeFromNib完成时子视图消失
我有一个带有NSBox子视图的视图(为shadow启用了CA),我正试图在awakeFromNib期间动态地将我的NSView子类添加到该框中 根据用户的不同,这些NSView的数量也会有所不同,因此我编写了一些代码来确定要生成多少行和列,以便在框中对它们进行良好的排列。(我已经考虑过NSMatrix,但它并没有按照我想要的方式进行布局) 问题是,在awakeFromNib方法完成后,子视图突然消失! 我已经放了大量的NSLog语句来尝试跟踪它,我所发现的是,子视图显然是在它们消失之前添加到NSBox中的。运行应用程序时,子视图会短暂出现,然后在动画完成后消失 以下是我的awakeFromNib方法:Objective c awakeFromNib完成时子视图消失,objective-c,cocoa,nsview,appkit,nsviewcontroller,Objective C,Cocoa,Nsview,Appkit,Nsviewcontroller,我有一个带有NSBox子视图的视图(为shadow启用了CA),我正试图在awakeFromNib期间动态地将我的NSView子类添加到该框中 根据用户的不同,这些NSView的数量也会有所不同,因此我编写了一些代码来确定要生成多少行和列,以便在框中对它们进行良好的排列。(我已经考虑过NSMatrix,但它并没有按照我想要的方式进行布局) 问题是,在awakeFromNib方法完成后,子视图突然消失! 我已经放了大量的NSLog语句来尝试跟踪它,我所发现的是,子视图显然是在它们消失之前添加到NS
-(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];
}
-(void)从NIB唤醒
{
//用每个公司对象的按钮填充数组
按钮=[[NSMutableArray alloc]init];
对于(BCCompany*c in[[[BCParser sharedParser]managedObjectContext]fetchObjectsForEntityName:@“BCCompany”,谓词为nil,sortdescriptor为nil])
{
CompanyButton*按钮=[[CompanyButton alloc]initWithFrame:NSMakeRect(0,0,150,150)];
[按钮设置公司名称:[c名称]];
[按钮添加对象:按钮];
}
//确定我们可以在窗口中容纳多少列--每侧填充100px--每个按钮的宽度为150px
int noOfColumns=([[self view]frame].size.width-200)/150;
如果([按钮计数] 对于NSView的文档中的(int i=0;i)-设置子视图:
此方法标记受影响的视图
以及需要显示的窗口区域。
如果您不调用-setNeedsDisplay
再次打开白盒
,会发生什么情况?顺便说一句:垃圾收集是打开的,如果它做出任何不同的更改,它们在awakeFromNib方法完成后仍然会消失。