[英]Connect outlet between table view and viewcontroller with storyboard
[英]How to use a segue with a table view but NOT a table viewController on my storyboard
好的,所以我一整天都在尋找解決方案,但沒有任何突出的方法可行。 當用戶選擇“vrijdag”(=荷蘭語的星期五)時,第二個ViewController會打開。
有什么用呢:我可以選擇鏈接到導航控制器的第一個viewcontroller並右鍵單擊並拖動到第二個Viewcontroller,而不是從我添加到第一個ViewController的Table View中拖動
我已經嘗試過多個教程,但它們都使用表視圖控制器,而不是帶有表視圖的視圖控制器:)
有人有解決方案嗎? 當然,重要的是我也知道單擊了哪個單元格,以便下一個Viewcontroller顯示正確的信息。
感謝您的時間 :)
使您的視圖控制器成為UITableViewDelegate
和UITableViewDataSource
。 然后在viewDidLoad
設置您的委托: self.myTableView.delegate = self
。
然后,只需在代碼中實現數據源和委托方法,尤其是以下方法:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:@"myCustomSegue" sender:indexPath];
}
編輯:這是如何在IB中設置手動segue。 首先,選擇您的第一個視圖控制器,控制+單擊並拖動到第二個視圖控制器,然后從小菜單中選擇“推送”。
然后,通過單擊segue本身(視圖控制器之間的直線上的圓圈)命名您的segue。 然后轉到右側的Attributes Inspector,您將看到一個名為Identifier的字段。 輸入您想要的名稱。
執行此設置后, performSegueWithIdentifier:sender:
方法應該可以正常工作。
還有一點需要注意:您可以在方法prepareForSegue:sender:
設置segue的工作方式。 這是一個示例實現。 用自定義類替換類和屬性名稱。 請注意,您可以使用此方法設置目標的公共屬性:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender //sender should be an NSIndexPath object
{
if ([segue.identifier isEqualToString:@"myCustomSegue"]) {
NSIndexPath *indexPath = sender;
MyViewController *secondViewController = (MyViewController*)segue.destinationViewController;
UITableViewCell* cell = [self tableView:self.tableView cellForRowAtIndexPath:indexPath]
secondViewController.name = cell.textLabel.text
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.