在我的MainViewController实现中,我需要从两个不同的类中访问变量。其中一个类是AppDelegate,另一个是FlipsideViewController。 我访问这些的方式是通过这段代码:
-(void)someMethod
{
MyApplicationAppDelegate *appDelegate = (MyApplicationAppDelegate *)[[UIApplication sharedApplication] delegate];
FlipsideViewController *viewController = (FlipsideViewController *)[[UIApplication sharedApplication] delegate];
然后我有一个我从我的应用程序委托访问的数组,以及一些从flipsideViewController中的UISwitch实例返回值的实例变量:
NSMutableArray* array = [[NSMutableArray alloc] initWithArray:(NSMutableArray *)appdelegate.originalArray];
for (id element in array)
{
if ([[element attribute] isEqualToString:@"someAttribute"] && [viewController.switch1 isOn] == YES)
{
//preform function
}
}
我不断收到错误消息“ - [MyApplicationAppDelegate switch1]:无法识别的选择器发送到实例。由于未捕获的异常而终止应用程序”
答案 0 :(得分:2)
[[UIApplication sharedApplication] delegate];将始终返回MyApplicationAppDelegate
类的(单例)实例,您不能简单地将其转换为FlipsideViewController*
。要访问flipsidecontroller值(假设它存储在你的appdelegate中),你可以定义一个属性并调用它:
-(void)somemethod{
MyApplicationAppDelegate *appDelegate = (MyApplicationAppDelegate *)[[UIApplication sharedApplication] delegate];
FlipsideViewController *viewController = appDelegate.flipsideController;
}