NSView 和动态加载子视图
为了了解我在做什么而不发布代码页...我有一个 NSOperation,我用它来处理添加到文件夹中的文件。在该 NSOperation 中,每当新作业启动时,我都会使用 NSNotificationCenter 向 NSView 发送通知。我的想法是,我想添加一个新的子视图来为我提供有关刚刚开始的作业的一些信息。问题是我似乎无法绘制新的子视图。这是我现在所拥有的。
- (void)drawRect:(NSRect)dirtyRect
{
NSLog(@"Draw Count %i", [jobViewArray count]);
int i = 0;
while (i < [jobViewArray count]) {
[self addSubview:[jobViewArray objectAtIndex:i]];
}
}
然后进一步向下:
-(void) newJobNotification: (NSNotification *) notification
{
if (!jobViewArray)
jobViewArray = [[NSMutableArray alloc] init];
++jobCount;
NSRect rect;
rect.size.width = 832;
rect.size.height = 120;
NSPoint point = { 0, ((jobCount * 120) - 120) };
rect.origin = point;
ProgressView *newJob = [[ProgressView alloc] initWithFrame:rect];
[jobViewArray addObject:newJob];
NSLog(@"Notice Count %i", [jobViewArray count]);
}
}
当我使用我的应用程序添加作业时,我的 NSView 正确接收了通知,子视图被正确添加到 jobViewArray
中,但是当 drawRect: 再次被调用,我的 jobViewArray 为空。这是我第一次尝试做这样的事情,所以我可能在这里做一些完全错误的事情......我想这是不言而喻的,因为它不起作用吧?
To get a sense of what I'm doing without posting pages of code... I have an NSOperation that I'm using to process files as they are added to a folder. In that NSOperation I'm using the NSNotificationCenter to send notifications to an NSView whenever a new job is started. The idea is, that I want to add a new subview to give me some information about the job that just started. The problem is I can't seem to get new subviews to draw. Here is what I have right now.
- (void)drawRect:(NSRect)dirtyRect
{
NSLog(@"Draw Count %i", [jobViewArray count]);
int i = 0;
while (i < [jobViewArray count]) {
[self addSubview:[jobViewArray objectAtIndex:i]];
}
}
and then further down:
-(void) newJobNotification: (NSNotification *) notification
{
if (!jobViewArray)
jobViewArray = [[NSMutableArray alloc] init];
++jobCount;
NSRect rect;
rect.size.width = 832;
rect.size.height = 120;
NSPoint point = { 0, ((jobCount * 120) - 120) };
rect.origin = point;
ProgressView *newJob = [[ProgressView alloc] initWithFrame:rect];
[jobViewArray addObject:newJob];
NSLog(@"Notice Count %i", [jobViewArray count]);
}
}
When I use my app to add a job, the notification is properly received by my NSView, the subview is properly added to the jobViewArray
, but then when drawRect:
gets called again my jobViewArray
is empty. It's the first time I've tried to do something like this so I'm probably doing something completely wrong here... I guess that goes with out saying since it doesn't work huh?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不应该将子视图添加到drawRect:中的视图中。当您收到通知时,您应该在那里添加子视图,因为第二次收到通知时,您将添加 2 个子视图,然后下一次添加 3 个子视图,依此类推。
如果您在通知中添加子视图,那么您将不需要弄乱数组。
You shouldn't be adding the subview to the view in drawRect:. When you receive the notification you should add the subviews there because the second time the notification comes around, you're going to add 2 subviews, then the next time 3 subviews and so one.
If you add the subview in the notification then you'll not need to mess around with the array.