【问题标题】:How Can I Access my ViewController's Property Variables From Another Class?如何从另一个类访问我的 ViewController 的属性变量?
【发布时间】:2012-12-13 06:52:06
【问题描述】:

好的,这确实困扰着我,我相信解决方案很简单......我无法从另一个类 (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

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

【问题讨论】:

    标签: iphone ios properties automatic-ref-counting


    【解决方案1】:

    在这行代码中:

    [ViewController setContactList:contactListCopy];

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

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

    [viewController setContactList:contactListCopy];

    【讨论】:

    • 谢谢@Levi。不过,在访问 ServerConnect.m 之外的属性变量时,我仍然遇到问题(请参阅下面的对话)。
    • 发布您的 AppDelegate .h 以及您当前尝试访问该属性的方式
    【解决方案2】:

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

    [ViewController setContactList:contactListCopy];
    

    你需要先创建一个 ViewController 类的实例,然后设置它的属性。像这样的:

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

    【讨论】:

    • 感谢 Edwin 的快速响应!当应用程序首次加载(“...didFinishLaunchingWithOptions...”)时,我确实从我的 AppDelegate 创建了 ViewController 类的实例: viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil];通过创建另一个实例,我不会丢失之前 viewController 实例中保存的所有数据(即其他实例变量)吗?我不能设置我之前在 AppDelegate 中声明的原始 viewController 实例的 contactList 吗?
    • 是的,这只是一个例子。如果您已经拥有该实例,请使用: [viewController setContactList:contactListCopy];如果您需要从 ServerConnect.m 访问它,请将其设置为应用程序委托的属性并通过那里访问它:[appDelegate.viewController setContactList:contactListCopy];
    • @JRoss 并确保您永远不要在实例名称的开头使用大写字母(例如 viewController 与类的 ViewController)。
    • @EdwinIskandar 我只是碰巧使 viewController 实例成为我的 AppDelegate 之前的属性(类似地在其 .h/.m 文件中),但是当我尝试上面的建议时,我收到以下错误: “语义问题:在‘AppDelegate’类型的对象上找不到属性‘viewController’”有什么想法为什么找不到 AppDelegate 的“viewController”属性?
    • @JRoss,您需要将您的 appDelegate 类导入 ServerConnect.m 并像这样引用您的应用程序委托(假设您的应用程序委托文件的名称是 AppDelegate): AppDelegate *appDel = (AppDelegate * )[UIApplication sharedApplication].delegate;
    猜你喜欢
    • 1970-01-01
    • 2015-04-04
    • 2015-06-16
    • 2017-10-01
    • 2011-03-27
    • 2013-11-28
    • 1970-01-01
    • 2020-03-05
    • 1970-01-01
    相关资源
    最近更新 更多