2011-10-24 63 views
0

我無法爲alertview設置操作事件。在這裏,當我點擊alertview按鈕時,它不能被操作。我的代碼中有什麼問題。無法設置UIAlertview事件?

這裏是我的代碼:

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

我有alertview只有一個按鈕,如 「OK」。但它沒有迴應。

+0

集alertview授人以自我 – SriPriya

+0

有你實現您的.h文件中的alertViewDelegate? –

+0

@SriPriya它工作得很好,謝謝。 –

回答

1

消息被髮送到您指定爲nil的委託。將代理設置爲self,它會起作用。

UIAlertView *view = [[UIAlertView alloc] initWithTitle:@"Warning" 
    message:@"Please enter all the details" 
    delegate:self 
    cancelButtonTitle:@"Dismiss" 
    otherButtonTitles:nil]; 
+0

沒有它沒有迴應 –

+0

謝謝它工作正常。謝謝 –

2

已經設置了一個以零警報意見委託,將其設置爲self,如果你沒有設置委託,委託方法不叫

0
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 
     } 
    }