【发布时间】:2012-04-18 07:59:50
【问题描述】:
我的主窗口的内容视图设置如下:
newContentView = [[CutoutView alloc]initWithFrame:window.frame];
[window setContentView:newContentView];
[newContentView release];
CutoutView 是我的子类的名称。现在我想给它添加一个子视图,所以我做了以下,
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];
但这对我需要做什么没有任何想法?谢谢!
【问题讨论】:
-
我通常只是添加一个属性来跟踪我以后想要访问的视图。
-
好的,我做了
@property (nonatomic, strong) FilterView *filterView;,但我如何访问另一个类中的该属性以获取其方法? -
假设它是您的应用程序委托的一部分,您只需获取应用程序委托的句柄,如
MyAppDelegateClass *appDelegate = [[UIApplication sharedApplication] delegate];,然后从中获取属性。 -
好的,所以对于mac来说它是
MyAppDelegateClass *appDelegate = [[NSApp sharedApplication] delegate];,然后我会做[appDelegate filterView],但是要获得方法,我应该在最后一段代码中添加什么?感谢您的帮助!
标签: objective-c xcode cocoa