2013-07-13 98 views
2

我試圖在按任意一個按鈕(1 & 2)後保持此UIAlertView仍然關閉警報

一旦我的「+」按鈕或按「 - 」鍵,我可以看到UILabel文本增量,那麼它會關閉UIAlertView

這是我目前使用的是什麼:

#pragma Alert View Methods 

-(void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated 
{ 
     [self dismissWithClickedButtonIndex:buttonIndex animated:animated]; 

} 

#pragma count functions 
-(void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex 
{ 
    if (buttonIndex == 1 || buttonIndex == 2) { 
     return; 
    } 
    else{ 

     [self dismissWithClickedButtonIndex:buttonIndex animated:YES]; 
    } 
} 

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    if (buttonIndex == 1) { 
     self.currentCountButtonCount++; 
     [self.countAlert setMessage:[NSString stringWithFormat:@"%d",self.countButtonCount + 1]]; 

    }if (buttonIndex == 2) { 
     self.currentCountButtonCount--; 
     [self.countAlert setMessage:[NSString stringWithFormat:@"%d",self.countButtonCount - 1]]; 

    } 
} 

- (IBAction)countClick:(id)sender { 


    // tallies and keeps current count number 

    if (!self.currentCountButtonCount) 
     self.currentCountButtonCount = 0; 

    NSString *alertMessage = [NSString stringWithFormat:@"%d", self.countButtonCount]; 

    self.countAlert = [[UIAlertView alloc]initWithTitle:@"Count" message:alertMessage delegate:self cancelButtonTitle:@"end" otherButtonTitles:@"+",@"-", nil]; 

    [self.countAlert show]; 
} 

在我的最後一個問題,有人告訴我,定製做出來,這是什麼現在我嘗試,它仍然駁回UIAlert。

我怎樣才能保持它的標籤更改,直到他們觸摸結束按鈕?

回答

2

你使用的是AlertView的默認按鈕,點擊該按鈕後,它會自動關閉alertview。

所以,你必須以編程方式創建您的按鈕,並補充說,在你的alertview按鈕,如:在這個BTN

UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 50)]; 
[btn setTitle:@"+" forState:UIControlStateNormal]; 
[countAlert addSubview:btn ]; 

調用你的方法。

所以你必須用「+」和「 - 」創建兩個自定義按鈕。並在AlertView中添加該按鈕。

-(void)setAlertValue:(id)sender{ 

    switch ([sender tag]) { 
     case 1: 
     { 
      // currentCountButtonCount++; 

      [self.countAlert setMessage:[NSString stringWithFormat:@"%d",++countButtonCount]]; 
     } 
      break; 
     case 2: 
     { 
      //currentCountButtonCount--; 

      [self.countAlert setMessage:[NSString stringWithFormat:@"%d",--countButtonCount]]; 
     } 
      break; 
     default: 
      break; 
    } 
} 

- (IBAction)countClick:(id)sender { 
// tallies and keeps current count number 

    if (!currentCountButtonCount) 
     currentCountButtonCount = 0; 

    NSString *alertMessage = [NSString stringWithFormat:@"%d", countButtonCount]; 

    self.countAlert = [[UIAlertView alloc]initWithTitle:@"Count" message:alertMessage delegate:self cancelButtonTitle:@"end" otherButtonTitles:nil, nil]; 


    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(10, 50, 40, 20)]; 
    [btn addTarget:self action:@selector(setAlertValue:) forControlEvents:UIControlEventTouchUpInside]; 
    [btn setBackgroundColor:[UIColor greenColor]]; 
    btn.tag = 1; 

    [btn setTitle:@"+" forState:UIControlStateNormal]; 

    UIButton *btn1 = [[UIButton alloc] initWithFrame:CGRectMake(230, 50, 40, 20)]; 
    [btn1 addTarget:self action:@selector(setAlertValue:) forControlEvents:UIControlEventTouchUpInside]; 
    [btn1 setBackgroundColor:[UIColor redColor]]; 
    btn1.tag = 2; 

    [btn1 setTitle:@"-" forState:UIControlStateNormal]; 
    [countAlert addSubview:btn]; 
    [countAlert addSubview:btn1]; 
    [self.countAlert show]; 

} 

enter image description here

+0

我會覆蓋哪些方法? @BaZinga – Keeano

+0

你根本不應該覆蓋警報。 – Sulthan

+0

@KeeanoMartin看到我編輯的答案 – KDeogharkar

0

我不想在你的遊行下雨,但如果你認爲一個警報視圖是處理一個變量的遞增/遞減的最佳方式,我會建議你要重新考慮你的設計。

UIAlertViews適用於瞬態信息和簡化決策。一個簡單的「你確定嗎?」是警報視圖用法的文本示例。

從用戶的角度來看,它更加舒適,能夠修改滑塊中的所有屬性或任何其他形式的永久輸入,然後,當確定時,用警報視圖(或確認屏幕)點擊確認按鈕, 。在Alert視圖中執行此操作不僅容易出錯,而且與iOS其餘部分的工作方式相反。

如果您在應用程序中安裝另一種輸入形式時遇到問題,請閱讀如何執行動畫並在需要時顯示控件,將輸入隱藏在UIAlertView中對您來說只是最簡單的解決方案,但不是最好的用戶。