0

in my MainViewController implementation, I need to access variables from two different classes. one of the classes is the AppDelegate and the other is the FlipsideViewController. the way I accessed these was through this code:

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

then I have an array I access from my application delegate, and some instance variables that return values from an instance of UISwitch from the 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
    }
}

I keep getting the error message "-[MyApplicationAppDelegate switch1]: unrecognized selector sent to instance. terminating app due to uncaught exception"

Vladimir
  • 170,431
  • 36
  • 387
  • 313
kevin Mendoza
  • 1,211
  • 2
  • 12
  • 16

1 Answers1

2

[[UIApplication sharedApplication] delegate]; will always return the (singleton) instance of MyApplicationAppDelegate class and you cannot simply cast it to FlipsideViewController*. to access flipsidecontroller value (assuming it is stored in your appdelegate) you can define a property and call it:

-(void)somemethod{
     MyApplicationAppDelegate *appDelegate = (MyApplicationAppDelegate *)[[UIApplication sharedApplication] delegate];
     FlipsideViewController *viewController = appDelegate.flipsideController;
}
Vladimir
  • 170,431
  • 36
  • 387
  • 313
  • the switch1 value is not stored in my appdelegate, it comes from the FlipsideViewController class. – kevin Mendoza Jan 16 '10 at 00:33
  • 1
    What is happening is that when you call `viewController.switch1` (same as `[viewController switch1]` viewController is not really your controller because you assigned it improperty. Instead viewController is set to MyApplicationAppDelegate because you assigned viewController here: `viewController = (FlipsideViewController *)[[UIApplication sharedApplication] delegate];` Basically you assigned the application delegate itself to viewController not the controller! You need something like `viewController = [[UIApplication sharedApplication] delegate].myViewController;` – Nimrod Jan 16 '10 at 01:46