2013-12-11 54 views
-1

我有一個uialertview內insertnewobject函數如下所示。UIAlertView委託裏面insertnewObject

- (void)insertNewObject:(UIButton *)sender 
{ 
    //NSLog(@"Hello"); 
    UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Add Reference" 
                 message:@"Enter Reference Manually" 
                delegate:nil 
              cancelButtonTitle:@"Insert Manually" 
              otherButtonTitles:nil]; 
    [message addButtonWithTitle:@"Enter ISBN"]; 
    [message addButtonWithTitle:@"Enter DOI"]; 
    [message show]; 
} 

我可以看到UIAlertView。但是這個功能根本沒有達到!

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    NSLog(@"Hello"); 
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex]; 
    if([title isEqualToString:@"Enter ISBN"]) 
    { 
     NSLog(@"Button 1 was selected."); 
    } 
    else if([title isEqualToString:@"Enter DOI"]) 
    { 
     NSLog(@"Button 2 was selected."); 
    } 
    else if([title isEqualToString:@"Insert Manually"]) 
    { 
     NSLog(@"Button 3 was selected."); 
    } 
} 

請讓我知道我哪裏去錯了!

回答

2

委託設爲

更新此行

UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Add Reference" 
                message:@"Enter Reference Manually" 
               delegate:self 
             cancelButtonTitle:@"Insert Manually" 
             otherButtonTitles:nil]; 
+0

謝謝你得到它的工作! – bharath

+0

太棒了!如果這是你正在看的答案,那麼接受答案:) – aToz

2

而不是使用標題proprty,您可以使用buttonIndex屬性這一直接,委託設爲自我,

試試這個

UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Add Reference" 
                  message:@"Enter Reference Manually" 
                 delegate:self 
               cancelButtonTitle:@"Insert Manually" 
               otherButtonTitles:nil]; 
     [message addButtonWithTitle:@"Enter ISBN"]; 
     [message addButtonWithTitle:@"Enter DOI"]; 
     [message show]; 

Alertview委託方法:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    NSLog(@"Hello"); 

    if(buttonIndex==1) 
    { 
     NSLog(@"Button 1 was selected."); 
    } 
    else if(buttonIndex==2) 
    { 
     NSLog(@"Button 2 was selected."); 
    } 
    else if(buttonIndex==0) 
    { 
     NSLog(@"Button 3 was selected."); 
    } 
} 
1

要設置delegate零時,delegate應該自行調用委託的方法。

0

您已設置delegate:nil然後它如何調用委託方法。只需將它設置爲delegate:self並看到魔法。

UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Add Reference" 
                message:@"Enter Reference Manually" 
               delegate:self //just change here 
             cancelButtonTitle:@"Insert Manually" 
             otherButtonTitles:nil]; 
相關問題