在我MainViewController实现,我需要从两个不同的类访问的变量。的类中的一个是所述的AppDelegate和另一个是FlipsideViewController。 我访问这些的方法是通过这样的代码:

-(void)someMethod
{
MyApplicationAppDelegate *appDelegate = (MyApplicationAppDelegate *)[[UIApplication sharedApplication] delegate];
FlipsideViewController *viewController = (FlipsideViewController *)[[UIApplication sharedApplication] delegate];

然后我有一个数组从我的应用程序的代理访问,和从UISwitch的实例从flipsideViewController返回值的一些实例变量:

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交换机1]:无法识别的选择发送到实例终止应用程序由于未捕获的异常。”

有帮助吗?

解决方案

[[UIApplication的sharedApplication]委托];将始终返回MyApplicationAppDelegate类的(单)实例,你不能简单地将它转换到FlipsideViewController*。访问flipsidecontroller值(假设它被存储在你的的appdelegate)可以定义一个属性,并调用它:

-(void)somemethod{
     MyApplicationAppDelegate *appDelegate = (MyApplicationAppDelegate *)[[UIApplication sharedApplication] delegate];
     FlipsideViewController *viewController = appDelegate.flipsideController;
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top