UIViewController对象的属性不可访问

时间:2011-11-17 22:27:29

标签: objective-c xcode uiviewcontroller

我正在使用xcode-4.2运行Mac OS X Lion

在我的应用程序的AppDelegate.m中:didFinishLaunchingWithOptions方法我有以下摘录。

UIViewController *viewController1 = [[WSWFirstViewController alloc] initWithNibName:@"WSWFirstViewController" bundle:nil];
UIViewController *viewController2 = [[WSWSecondViewController alloc] initWithNibName:@"WSWSecondViewController" bundle:nil];
self.tabBarController = [[TabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];

self.window.rootViewController = self.tabBarController;

[self.tabBarController showSplash];

[viewController1 displayItems];

我要做的是访问viewController1对象的displayItems属性,该对象属于WSWFirstViewController类型。

在WSWFirstViewController.h中我有

@property (retain, nonatomic) NSMutableArray *displayItems;

在WSWFirstViewController.m中,我有@synthesized displayItems。

我似乎无法弄明白为什么我会得到:

在'UIViewController *'类型的对象上找不到属性'displayItems'。

直到我注意到我的对象(viewController1)不是类型UIViewController *而是WSWFirstViewController *。但这仍然无法帮助我。

感谢任何建议!

2 个答案:

答案 0 :(得分:1)

您应该按照正确的类型推荐您的控制器:

 WSWFirstViewController *viewController1 = [[WSWFirstViewController alloc] initWithNibName:@"WSWFirstViewController" bundle:nil];
 WSWSecondViewController *viewController2 = [[WSWSecondViewController alloc] initWithNibName:@"WSWSecondViewController" bundle:nil];

 ....

 [viewController1 displayItems];  // this will now work if you are in the same context

答案 1 :(得分:0)

你是否在界面NSMutableArray中声明了displayItems?