2013-12-11 4 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

набор делегата самостоятельно

Update эта линия

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]; 
Смежные вопросы