2011-11-30 77 views
0

我有一個表視圖作爲我的RootViewController,並用此代碼按下按鈕上的另一個按鈕。未在新視圖控制器中調用按鈕操作

[[self navigationController] pushViewController:aboutVC animated:YES]; 

我的問題是,我的按鈕的操作時,他們被按下時不會被調用。我已經檢查了幾乎所有我能想到的內容,並且我非常肯定地看到有一個視圖超出了攔截用戶交互的按鈕。除了在我的新視圖控制器中編碼的按鈕及其動作,我沒有別的。我正在使用一個自定義單元格,但其中沒有任何內容。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    tableView.separatorStyle = UITableViewCellSeparatorStyleNone; 
    cell.userInteractionEnabled = NO; 

    //ADD BUTTONS 
    UIButton *redButton = [[UIButton alloc] init]; 
    redButton.backgroundColor = [UIColor colorWithRed:0.0/255 green:0.0/255 blue:0.0/255 alpha:1.00]; 
    [redButton addTarget:self action:@selector(redButton:) forControlEvents:UIControlEventTouchUpInside]; 
    redButton.frame = CGRectMake(9, 44, 40, 39); 

    [cell addSubview:redButton]; 

    [redButton release]; 
    return cell; 
} 


- (void)redButton:(id)sender 
{ 
    //Button action will go here 
} 

關於我可能會做錯什麼的任何想法?

+1

設置cell.userInteractionEnabled = YES;然後動作會調用,並像這樣添加按鈕[cell.contentView addSubview:redButton]; – Narayana

+0

@Narayana - 我剛要回答,但您的評論首先出現。你能發表答覆嗎? – bryanmac

+0

我嘗試過,沒有運氣。據我瞭解,單元不需要啓用用戶交互,因爲按鈕用戶交互是一個單獨的設置。這是我在我的RootViewController中做到的,它工作正常。 – user1072515

回答

2
cell.userInteractionEnabled = NO; 
[cell addSubview:redButton]; 

替換上面下方2行的代碼行,將工作

cell.userInteractionEnabled = YES; 
[cell.contentView addSubview:redButton]; 
+0

+1 - 還請注意,UITableViewCell繼承自UIView這是什麼提供userInteractionEnabled – bryanmac

+0

我試過這個,它仍然無法正常工作。該按鈕正在顯示,但未被調用。也許它不清楚,但我在cellForRowAtIndexPath中添加按鈕,而不是在CustomCell類中,如果這樣做有所作爲。 – user1072515

+0

你改變了這一行[cell.contentView addSubview:redButton]; – Narayana

相關問題