无法从其他类访问@Property [英] Can't access @Property from other class

查看:197
本文介绍了无法从其他类访问@Property的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法访问我的程序中的一些变量。

I'm having trouble accessing some variables in my program.

我有一个类叫MainMenu。

I have one class called MainMenu.

在.h文件中,我声明了2个属性,如下所示:

In the .h file I have declared 2 properties as follows:

MainMenu.h:
@property (nonatomic, retain) IBOutlet NSView *mainView;
@property (nonatomic, retain) IBOutlet NSWindow *theMainWindow;

在另一个类文件中,我想要能够访问这两个变量,下面的代码在另一个.h类文件中不起作用,我不明白我做错了什么:

In another class file, I want to be able to access these 2 variables, current I am using the following code in the other .h class file which does not work, I don't understand what I'm doing wrong:

AppDelegate.m:
MainMenu *theMainMenu = [[MainMenu alloc] init];
[theMainMenu switchViews:theMainMenu.theMainWindow:theMainMenu.mainView];

这里我创建一个MainMenu类的对象,并调用一个名为'switchViews' ,然后我想传递它我无法访问的2个变量。

Here I create an object of the MainMenu class, and invoke a method called 'switchViews' in its definition, I then want to pass it the 2 variables which I'm having trouble accessing.

任何帮助非常感激。
先感谢大家。

Any help greatly appreciated. Thanks in advance everyone.

编辑:'switchViews'方法如下所示:

'switchViews' method shown below:

- (void)switchViews:(NSWindow*)mainWindow:(NSView*)newView {
    NSView *dummyView;
    [mainWindow setContentView:dummyView];
    [mainWindow setContentSize:newView.frame.size];
    [mainWindow setContentView:newView];
}


推荐答案

的MainMenu类的2个不同实例。如果你连接你的IBOutlets,你必须有一个蓝色立方体在IB设置到您的MainMenu类,正确吗?但是,当您在应用程序委托中分配init时,会创建另一个没有连接到任何属性的MainMenu实例。相反,您还应该在您的应用程序委托中设置一个蓝色立方体,并在该类中有一个IBOutlet,您可以连接到IB中的MainMenu蓝色立方体。

I think the problem is one of 2 different instances of your MainMenu class. If you hooked up your IBOutlets, you must have a blue cube in IB set to your MainMenu class, correct? However, when you alloc init one in your app delegate, that creates another instance of MainMenu that doesn't have those properties connected to anything. Instead, you should also have a blue cube in IB set to your app delegate, and have an IBOutlet in that class that you connect to the MainMenu blue cube in IB.

这篇关于无法从其他类访问@Property的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆