2012-10-12 32 views
1

我正在開發應用程序,我有一個需求,即有傳入呼叫時相應的方法被調用。我寫了alertview代碼,其作品完美並顯示了alertview。UIAlertView委託方法沒有在iphone中調用

Alertview包含兩個按鈕接受和拒絕,當我點擊這些按鈕中的任何一個時,不調用alertview委託方法。

+ (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"); 
    } 

} 

我打電話給+(void)incomingcall方法從另一種方法使用主線程。

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

我寫協議類即<UIAlertViewDelegate>但委託方法不叫任何一個可以解決我的問題,在此先感謝。

回答

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

在類方法的情況下,self指的是類本身,而不是對象的實例(如何將一個類的方法知道的類的實例?)。因此,您必須將incomingCallAlertView:方法變爲實例方法(即,在showIncomingCalling方法中,使用負號而不是加號的前綴並在showIncomingCalling方法中調用self類名的插入項),或者實施委託方法,就好像它們是類方法:

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

(這不工作因爲類對象本身是它的元類的實例,這意味着該類方法是元類的真的只是實例方法)

等。

順便說一句,仔細閱讀一個體面的Objective-C教程和/或語言參考。這個問題不應該在這裏提出,因爲它太基本,不能在其他資源中查找。

+0

謝謝你幫了很多 – smoothumut

相關問題