1
votes

I am developing am application in that,i have one requirement i.e when there is incoming call corresponding method is called.I write alertview code,its works perfectly and shows alertview.

Alertview contains two button accept and reject ,when i click any of these buttons alertview delegate methods are not called.

+ (void)incomingCallAlertView:(NSString *)string
{
    UIAlertView *callAlertView=[[UIAlertView alloc] initWithTitle:string message:@""   delegate:self cancelButtonTitle:@"reject" otherButtonTitles:@"accept",nil];
    [callAlertView show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{

    NSLog(@"clickedButtonAtIndex");

    if(buttonIndex==0)
    {
          NSLog(@"buttonindex 0");
    }
    else
    {
          NSLog(@"buttonindex 1");
    }

}

I am calling the +(void)incomingcall method from another method using main thread.

- (void)showIncomingcalling
{
     [CallingViewController performSelectorOnMainThread:@selector(incomingCallAlertView:)      withObject:@"on_incoming_call" waitUntilDone:YES];
}

I write protocol in class ie <UIAlertViewDelegate> but the delegate methods are not called can any one solve my problem thanks in advance.

1

1 Answers

19
votes
initWithTitle:string message:@"" delegate:self
                                           ^^
                                      Here it is!

In the context of a class method, self refers to the class itself, and not an instance of an object (how would a class method know about instances of the class?). So you must either make the incomingCallAlertView: method into an instance method (i. e. prefix it with a minus sign instead of a plus sign and call self insetad of the class name in the showIncomingCalling method), or implement the delegate methods as if they were class methods:

+ (void)alertView:(UIAlertView *)av clickedButtonAtIndex:(NSInteger)index

(This does work because the class object itself is an instance of its metaclass, and that means that class methods are really just instance methods of the metaclass.)

etc.

By the way, read attentively a decent Objective-C tutorial and/or language reference. This question should not have been asked here, as it's too basic for not being looked up in other resources.