gpt4 book ai didi

objective-c - 从另一个 subview 获取 subview

转载 作者:行者123 更新时间:2023-12-03 17:42:09 24 4
gpt4 key购买 nike

我的主窗口内容 View 设置如下:

 newContentView = [[CutoutView alloc]initWithFrame:window.frame];
[window setContentView:newContentView];
[newContentView release];

其中 CutoutView 是我的子类的名称。现在我想向其添加一个 subview ,因此我执行了以下操作,

filterView = [[FilterView alloc]initWithFrame:NSMakeRect(100, 100, 500, 500)]; 
[newContentView addSubview:filterView];
[filterView release];

一切工作正常,除了现在我想从我的filterView子类调用方法,并且我想像这样得到它,但它不会工作。

FilterView *filter = [[NSApp delegate] contentView]; 

我在文档中读到,通过使用 contentView 它只会“返回窗口层次结构中最高的可访问 NSView 对象”,因此我尝试将以下内容添加到 addSubview

[newContentView addSubview:filterView positioned:NSWindowAbove relativeTo:nil];

但这对我需要做什么没有任何想法?谢谢!

最佳答案

内容 View 实际上是您的 CutoutView 类,因此您应该使用:

FilterView *filterView = [[[[[NSApp delegate] window] contentView] subviews] objectAtIndex:0];

但是有两种更干净的方法是:

1) 使用 IBOutlets 跟踪您的 View 并通过 IB 分配它们。2)使用标签:

filterView.tag = 101;

然后使用:

FilterView* filterView = [[[NSApp delegate] contentView] viewWithTag:101];

关于objective-c - 从另一个 subview 获取 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10197258/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com