Не можете установить события UIAlertview? - PullRequest
0 голосов
/ 24 октября 2011

Я не могу установить действие для оповещения.Здесь, когда я нажимаю на кнопку alertview, она не может быть выполнена.В чем проблема в моем коде.

Вот мой код:

-(IBAction)savebuttons
{
if([username.text isEqualToString:@""] && [password.text isEqualToString:@""] && [emailid.text isEqualToString:@""] && [phonenum.text isEqualToString:@""] && [address.text isEqualToString:@""] && [city.text isEqualToString:@""] && [state.text isEqualToString:@""] && [country.text isEqualToString:@""] && [zipcode.text isEqualToString:@""]) {
    UIAlertView *view = [[UIAlertView alloc] initWithTitle:@"Warning" message:@"Please enter all the details" delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
    [view show];
    [view release];
}   
else if([username.text isEqualToString:@""])
{
    views = [[UIAlertView alloc] initWithTitle:@"Email ID" message:@"Email ID cannot be blank" delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
    views.tag=1;
    [views show];
    [views release];

}
else if([password.text isEqualToString:@""]){
    UIAlertView *view = [[UIAlertView alloc] initWithTitle:@"Password" message:@"Password cannot be blank" delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
    view.tag=2;
    [view show];
    [view release];
}
   else
   {

    .....
     ......
}

- (void)alertView:(UIAlertView *)alertViews clickedButtonAtIndex:(NSInteger)buttonIndex
{
      if(alertViews.tag == 1)
      {
          if(buttonIndex == 0)
          {
    [username becomeFirstResponder];
    NSLog(@"Username");
         }
     }
     if(alertViews.tag == 2)
      {
          if(buttonIndex == 0)
          {
    [password becomeFirstResponder];
    NSLog(@"Password");
         }
}

У меня есть только одна кнопка для просмотра предупреждений, например, "ОК".Но это не отвечает.

Ответы [ 3 ]

2 голосов
/ 24 октября 2011

вы установили делегат представлений предупреждений равным nil, установите его на self, если вы не установили делегат, методы делегата не вызываются

1 голос
/ 24 октября 2011

Сообщение отправляется делегату, которого вы указали как ноль. Установите делегата на себя, и он будет работать.

UIAlertView *view = [[UIAlertView alloc] initWithTitle:@"Warning" 
    message:@"Please enter all the details" 
    delegate:self 
    cancelButtonTitle:@"Dismiss" 
    otherButtonTitles:nil];
0 голосов
/ 24 октября 2011
UIAlertView *myAlert = [[UIAlertView alloc]
               initWithTitle:@"Alert !!" message:@"This is my Alert" 
               delegate:self cancelButtonTitle:@"Cancel"                     
               otherButtonTitles:@"OK",nil];
   [myAlert show];
   myAlert.tag = 1;
   [myAlert release];


   // Delegate Method of UIAlert :
  - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  {
      if (buttonIndex==1 && myAlert.tag == 1) 
       {
            // Action to be done/ called on button index 1 (OK button) of myAlert
       }
      else if (buttonIndex==0 && myAlert.tag == 1)
       {
            // Action to be done/ called on button index 0 (Cancel button) of myAlert
       }
  }
...