如何从另一个类访问我的ViewController的属性变量?

时间:2012-12-27 22:35:21

标签: iphone ios properties automatic-ref-counting

好吧,这真的让我烦恼,我确信解决方案很简单......我无法从另一个类(SeverConnect.m)设置我的ViewController的属性变量,我已经在我的ViewController中正确地声明和合成了它。 h / .m文件:

ServerConnect.h:

#import <Foundation/Foundation.h>
#import "ViewController.h"
#import "Contact.h"

@class ViewController;

@interface ServerConnect : NSObject
{
Contact *newContact;
NSString *codeRawContent;
NSMutableArray *contactListCopy;
...  //Other variables declared here, but not shown in order to save space

内部ServerConnect.m:

- (void)parserDidEndDocument:(NSXMLParser *)parser
{
NSLog(@"parserDidFinish");

newContact = [[Contact alloc] initWithCodeInfo:(NSString *)codeInfo
                                       contactName:(NSString *)completeName 
                                      contactImage:(UIImage *)profileImage
                                    contactWebSite:(NSString *)codeRawContent];
[contactListCopy insertObject:newContact atIndex:0];
[ViewController setContactList:contactListCopy];  //Automatic Reference Counting Error Occurs Here: "No known class method for selector 'setContactList:'"
}

正如我上面提到的,我已经在我的ViewController的.h / .m文件中声明并合成了属性变量“contactList”(没有错误):

@property (nonatomic, retain) NSMutableArray *contactList;  //In ViewController's .h file
@synthesize contactList;  //In ViewController's .m file

有人能告诉我我做错了什么吗?提前感谢您提供的任何帮助!

2 个答案:

答案 0 :(得分:1)

您正在尝试访问类的实例属性:

[ViewController setContactList:contactListCopy];

您需要首先创建ViewController类的实例,然后设置其属性。像这样:

ViewController *viewController = [[ViewController alloc] init];
[viewController setContactList:contactListCopy];

答案 1 :(得分:0)

在这行代码中:

[ViewController setContactList:contactListCopy];

您应该使用ViewController类型的变量。你使用它的方式,它应该是一个类方法,而不是一个属性。 写下类似的东西:

ViewController *viewController = [[ViewController alloc] init];

[viewController setContactList:contactListCopy];