views:

266

answers:

2

Hi, I'm new to obj C, I come from an AS3.0 background. Basically in this app I'm making, I'm using the applicationWillResignActive in the app delegate to react to a call coming in . I have a navigationController with a table view that just triggers another view on select (the template provided in xcode basically) I have a method in my AnotherViewController view that gets loaded from the row select. That method will (for now) change my label in there to "Call coming in". In my applicationWillResignActive I'm calling this method and it works fine but I get an annoying warning saying UIViewController may not be able to respond to handleIncomingCall...but it compiles fine and DOES respond to it. How can I get rid of this warning sign?

-(void) applicationWillResignActive:(UIApplication *)application {
    NSLog(@"CALL COMIN IN");
    UIViewController *vc = [navigationController visibleViewController]; 
    [vc handleIncomingCall];
    [vc release];
}

Sorry, I'm trying to figure out how to format it in here...

A: 

The reason is because you're telling the compiler the object is of UIViewController and handleIncomingCall is indeed not a method of UIViewController. You can get rid of the warning my including the .h file and defining the object as AnotherViewController or whatever you wound up calling it. The delegate will see in the .h file that handleIncomingCall is a method of that class and therefore will stop giving you that warning.

Obj-C is not a strongly typed language, which is why it compiles. It works because though you define the variable as UIViewController it actually of your own type that responds to the appropriate message. But there is no way for the compiler to know this at compile time.

Joel B
Hey,Thanks for the reply. This is about to where I got. I figured out that it was indeed trying to call my method on UIViewController so I did cast it to AnotherViewController. Problem is that any number of view controllers may be visible during this event, not just AnotherViewController. Any suggestions on how I could write this out? Any way to check what type is currently visible and act accordingly?
bmanderscheid
A: 

OK, so I'm doing this:

AnotherViewController *vc = (AnotherViewController *) navigationController.visibleViewController;
    if ([vc isKindOfClass:[AnotherViewController class]]){
     [vc handleIncomingCall];
    }
    else{
     NSLog(@"NOT ANOTHER VIEW CONTROLLER DO NOTHING");
    }

This will work great for this application, I'm just wondering how you would handle it if there were multiple views with that method and you wanted to call it. I don't imagine you would add a bunch of ||'s to the if statement. I'll cross that bridge when I get there. Thanks a ton for the help!!

bmanderscheid