2012-11-23 57 views
2

我添加了一個按鈕,在我的表視圖的細胞像這 -使用參數調用UIAlertView委託方法?

UIButton* checkBoxBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 
[checkBoxBtn setBackgroundImage:[UIImage imageNamed:@"check_normal.png"] forState:UIControlStateNormal]; 
    [checkBoxBtn addTarget:self action:@selector(checkBoxAction:) forControlEvents:UIControlEventTouchUpInside]; 
    checkBoxBtn.tag = indexPath.row; 
    [cell.contentView addSubview:checkBoxBtn]; 

,這是我checkBoxAction

-(void) checkBoxAction: (UIButton *)sender 
{ 
    NSInteger i =sender.tag + 1; 
    float perc = (i*100/18.0); 
    NSLog(@"%.2f",perc); 
    NSString* percentageStr = [[NSString alloc] initWithFormat:@"%3.2f%%(%d out of 18)",perc, i]; 
    barTitle.text = percentageStr; 
    barFGImage.hidden = NO; 
    if (perc == 100.00) { 
     [barFGImage setFrame:CGRectMake(barFGImage.frame.origin.x, barFGImage.frame.origin.y, 276.0, barFGImage.frame.size.height)]; 
    } 
    else 
    [barFGImage setFrame:CGRectMake(barFGImage.frame.origin.x, barFGImage.frame.origin.y, 280*perc/100, barFGImage.frame.size.height)]; 
    [sender setBackgroundImage:[UIImage imageNamed:@"check_select.png"] forState:UIControlStateNormal]; 
    sender.userInteractionEnabled = NO; 
} 

而且每一件事情好現在,但我想顯示警報當用戶按下checkBoxBtn,如果用戶按OK鍵則checkBoxAction應該called.I知道我們有UIAlertView委託方法

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 

但問題是如何獲取checkBoxBtn呢?

編輯:它與Midhun MP的回答OK,但我想在我的手機多了一個東西─我有三個標籤和按鈕like this screen shot,對複選框單擊希望由於在6更改標籤」天「以及我們在checkBoxAction方法中完成的其他內容 請幫助我。謝謝。

+0

我編輯的問題,請檢查 – Blios

回答

3

爲實現此目的,您需要實施- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex委託方法。

你可以不喜歡它:

在.H 像聲明一個UIButton實例:

UIButton *button; 

更改按鈕添加一個類似方法:

UIButton* checkBoxBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 
[checkBoxBtn setBackgroundImage:[UIImage imageNamed:@"check_normal.png"] forState:UIControlStateNormal]; 
[checkBoxBtn addTarget:self action:@selector(showAlert:) forControlEvents:UIControlEventTouchUpInside]; 
checkBoxBtn.tag = indexPath.row; 
[cell.contentView addSubview:checkBoxBtn]; 


- (void) showAlert:(id)sender 
{ 
    button = (UIButton *)sender 
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Title" message:@"Message" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: @"Cancel"]; 
} 

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    if(buttonIndex == 0) 
    { 
     [self checkBoxAction:button]; 
    } 
} 

用於獲取標籤:

給標籤添加標籤cellForRowAtIndexPath:這樣的:

statusText.tag = 7; 
[cell.contentView addSubview:statusText]; 

而且你可以用下面的代碼獲取標籤:

UILabel *tempLabel = (UIlabel *)[[button superview] viewWithTag:7]; 
tempLabel.text = @"Midhun"; 
+0

其工作..謝謝..噸 – Blios

+0

@Blios:高興:) –

+0

我也有一個單元格上的標籤,我想像我們這樣按鈕訪問它,所以必須發送另一個參數?請你解釋一下嗎? – Blios

相關問題