繁体   English   中英

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

[英]How Can I Access my ViewController's Property Variables From Another Class?

好的,这确实使我烦恼,并且我确定解决方案很简单...我无法从另一个类(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

有人可以告诉我我在做什么错吗? 预先感谢您提供的任何帮助!

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

[ViewController setContactList:contactListCopy];

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

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

在这行代码中:

[ViewController setContactList:contactListCopy];

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

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

[viewController setContactList:contactListCopy];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM