2012-06-04 71 views
2

你好,下午好,我在這裏有一些問題,說實話,我不明白 我必須爲不同的消息創建不同的alertViews爲同一屏幕,這些警報大多數只有1個按鈕,但有一個刪除需要2個按鈕,事情是,因爲其他人只有1個按鈕,當我創建2按鈕screenview和我添加(無效)alertView:(UIAlertView *)alertView clickedButtonAtIndex :(NSInteger的)buttonIndex方法,我有一些問題多個alertViews創建錯誤

這裏的一些代碼

- (IBAction)saveInfo{ 
if (med.text.length ==0) { 
    UIAlertView *alertViewError = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"ERROR",@"") 
                 message:NSLocalizedString(@"EMPTY1",@"") 
                 delegate:self 
               cancelButtonTitle:@"OK" 
               otherButtonTitles: nil]; 
    [alertViewError show]; 
    [alertViewError release]; 
} 
else if(medicamento.text.length >= 41){ 
    [self lenghtError:40:NSLocalizedString(@"TF_MED",@"")]; 
} 
else if (med.text.length ==0 || descripcion.text.length == 0) { 
    UIAlertView *alertViewError = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"ERROR",@"") 
                 message:NSLocalizedString(@"EMPTY2",@"") 
                 delegate:self 
               cancelButtonTitle:@"OK" 
               otherButtonTitles: nil]; 
    [alertViewError show]; 
    [alertViewError release]; 
} 
else if (descripcion.text.length >= 41){ 
    [self lenghtError:40:NSLocalizedString(@"TF_DESCRIPCION",@"")]; 
} 
else{ 
    [self insertDictionary]; 
    UIAlertView *alertViewAcept = [[UIAlertView alloc] initWithTitle:@"" 
                 message: NSLocalizedString(@"ACCEPT_MSG",@"") 
                 delegate:self 
               cancelButtonTitle:@"OK" 
               otherButtonTitles: nil]; 
    [alertViewAcept show]; 
    [alertViewAcept release]; 
    [self.navigationController popViewControllerAnimated:YES]; 
} 

}

- (IBAction)cancelData{ 
UIAlertView *alertViewCancel = 
[[UIAlertView alloc] initWithTitle: NSLocalizedString(@"BT_DELETE_MED",@"") 
        message: NSLocalizedString(@"MSG_DELETE_MED",@"") 
        delegate:self 
      cancelButtonTitle:@"OK" 
      otherButtonTitles: @"Cancel", nil]; 
[alertViewCancel setTag:999]; 
[alertViewCancel show]; 
[alertViewCancel release]; 

}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
if (alertView.tag == 999) { 
    if(buttonIndex==0){ 
     [self.Bayer_DB_obj deleteRowWithKeyValue:[NSString stringWithFormat:@"%d",IdMed] onKeyName:@"id_ctl_med" onTable:@"ctl_med"]; 
     // code to delete here 
     [self.navigationController popViewControllerAnimated:YES]; 
    } 
} 

}

所以,在第一部分中,我創造了一些警告,以表明他/她犯了一個錯誤,在第二部分用戶,我需要在刪除前進行確認,但在這裏,我需要2個按鈕,然後在第三部分中,我已經調用了方法,爲警報添加了一個標記,以避免在所有警報中進行比較,是,當你顯示alertViewAcept時,它會帶你到前一個視圖控制器,並在你點擊OK按鈕(這實際上是cancelbuttontitle)沒有任何「錯誤消息」

,所以我不知道我做錯了應用程序崩潰,請幫助

+0

什麼是「alertViewCancelar」? –

+0

對不起,當我發佈這個問題時,我編輯了一些變量名,我剛修好了,謝謝修改 –

回答

1

我想這個問題是你設置的代表alertViewAcept,並且在顯示警報之後,您彈出viewController,因此您的代理將被釋放,一旦單擊警報視圖上的某個按鈕,該代理就會給您一個錯誤。

你應該這樣做:

UIAlertView *alertViewAcept = [[UIAlertView alloc] initWithTitle:@"" 
                 message: NSLocalizedString(@"ACCEPT_MSG",@"") 
                 delegate:nil 
               cancelButtonTitle:@"OK" 
               otherButtonTitles: nil]; 

更妙的是,所有的警報,只有具備確定按鈕,不需要委託。在這種情況下,你甚至不需要標籤。

+0

實際上,就是這樣,我剛剛注意到了,謝謝:D –