awakeFromNib 完成后子视图消失
我有一个带有 NSBox 子视图的视图(启用了 CA 阴影),并且我尝试在 awakeFromNib 期间动态将我的 NSView 子类添加到框中。
根据用户的不同,这些 NSView 的数量也会有所不同,因此我编写了一些代码来确定需要多少行和列才能将它们很好地排列在框中。 (我已经考虑过 NSMatrix,但它没有按照我想要的方式布局)
问题是,在 awakeFromNib 方法完成后,子视图突然消失! 我已经放置了大量 NSLog 语句来尝试跟踪它,我发现的只是子视图在消失之前显然已添加到 NSBox 中。运行应用程序时,子视图会短暂出现,然后在动画完成后消失。
这是我的 awakeFromNib 方法:
-(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];
}
对此的任何帮助将不胜感激!
史蒂夫
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 NSView 的
-setSubviews 文档中:
如果您不在
whiteBox
上再次调用-setNeedsDisplay
会发生什么?In the documentation for NSView's
-setSubviews:
What happens if you don't call
-setNeedsDisplay
again onwhiteBox
?