2012-06-18 36 views
3

在我的應用程序中,我有一個ParentViewController,它有幾個元素。其中一個元素是UITableView。UITableViewCell中的UIButton執行Segue到另一個UIViewController使用Storyboard

我使用動態原型單元設計了表格的單元格,這是我在故事板中設計的。

含義,我在故事板中有一個UIViewController;裏面有一個UITableView;在UITableView裏面我有一個Prototype Cell。

現在,我在Prototype Cell中添加了一個UIButton,創建了另一個UIViewController(讓我們稱之爲ChildViewController),並將故事板中的UIButton和ChildViewController與Segue連接起來。

我應該如何編寫用戶單擊UIButton時激活Segue的代碼?

我設法創建表本身,並有動態單元格內的數據(他們有一些UILabels放在UIButton旁邊),但我無法弄清楚如何正確地將UIButton連接到代碼。

我不想使用UITableView默認的「選擇」功能,因爲我希望在動態單元格內有幾個按鈕 - 每個按鈕都會派生出不同的Segue給不同的ChildViewController。

回答

1

下面是我解決同樣的問題。希望它會有所幫助。

要做到這一點,你可以將指針傳遞給你的UITableViewController到您的自定義單元格的初始化方法,並將其存儲有:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
      // ... 
      CDTrackCell *cell = [tableView dequeueReusableCellWithIdentifier:strCellTableIdentifier]; 
      CDTrack *currTrack = [self.fetchedResultsController objectAtIndexPath:indexPath]; 

      [cell configureView:currTrack inTableViewController:self]; 

      return cell; 
    } 

保存指針在自定義的UITableViewCell類:

- (void) configureView:(CDTrack*)track 
inTableViewController:(CDTracksViewController*)_tvc 
{ 
    self.tvc = _tvc; 
    // ... 
} 

然後,您可以在點按按鈕時執行一次搜索:

- (IBAction)buttonAddToTracklistTapped:(UIButton *)sender { 
    [self.tvc performSegueWithIdentifier:@"showAddToTracklist" sender:self]; 
} 

任何更好的想法?

+0

另一個很好的方法是利用「委託」模式,我現在就試試:) –

相關問題