問題描述
我有一個小應用程序,它對初始表格視圖使用多個部分布局.一個部分顯示 Twitter 的最新趨勢,另一部分顯示 Twitter 的最新故事.當我點擊趨勢列表中的一個項目時,我會轉換到一個新的表格視圖控制器,該控制器顯示有關該趨勢的最新推文.在故事部分的根控制器中,我希望能夠在包含圖像、鏈接等的不同視圖控制器中顯示更多信息.問題是,當我在故事部分中選擇任何內容時,我會被推送到為趨勢部分設置的表格視圖控制器.我已經為每個 segue 命名并為我想要轉換到的兩個視圖都有自定義類,我這樣做是為了檢查正在調用哪個 segue:
I have a small app that uses multiple section layouts for the initial table view. One section displays the most recent trends from Twitter and the other section displays the most recent stories from Twitter. When I click on an item within the list of trends I transition to a new table view controller that displays the most recent tweets about that trend. Within the root controller for the stories section I wat to be able to display more information in a different view controller that contains images, links, and so forth. The problem is that when I select anything within the stories section, I am being pushed to the table view controller that is set up for the trends section. I have named each segue and have custom classes for both of the views that I want to transition to and I am doing this to check which segue is being called:
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if([[segue identifier] isEqualToString:@"viewTrendsSearch"]) {
//get the controller that we are going to segue to
SearchTrendResultsViewController *strvc = [segue destinationViewController];
//get the path of the row that we want from the table view
NSIndexPath *path = [self.tableView indexPathForSelectedRow];
//here we get the trend object from the array we set up earlier to hold all trends
Trends *results = [currentTrends objectAtIndex:[path row]];
//pass the object that was selected in the table view to the destination view
[strvc setQuery: results];
}
if([[segue identifier] isEqualToString:@"storyfullDetails"]) {
StoriesViewController *svc = [segue destinationViewController];
NSIndexPath *path = [self.tableView indexPathForSelectedRow];
Stories *results = [currentStories objectAtIndex:[path row]];
[svc setStory:results];
}
}
關于如何獲得不同觀點的任何建議?
Any suggestions on how to get to the different views?
推薦答案
您的問題中沒有足夠的信息可以確定,但這聽起來像是一個問題,我稱之為自動與手動 segues 以及每個問題的限制.
There's not quite enough information in your question to be sure, but this sounds like an issue with what I'd call automatic versus manual segues and the restrictions on each.
自動轉場是在 IB 中通過從(原型)表格單元格或其他控件拖動來創建的.它的好處是它是自動的——點擊控件執行 segue,你需要在代碼中執行 prepareForSegue:sender:
以便目標視圖控制器獲取正確的數據.缺點是任何給定的控件(包括原型表單元格)只能有一個傳出 segue(否則,故事板將不知道要自動執行哪個).
An automatic segue is created in IB by dragging from a (prototype) table cell or other control. The nice thing about it is that it's, well, automatic -- tapping the control performs the segue, and all you need to do in your code is implement prepareForSegue:sender:
so that the destination view controller gets the right data. The downside is that any given control (including prototype table cells) can only have one outgoing segue (otherwise, the storyboard wouldn't know which to automatically perform).
通過從源視圖控制器拖動,在 IB 中創建了一個 手動 segue.這樣做的好處是視圖控制器可以有多個傳出的segue.另一方面,它們與可點擊控件無關,因此您必須實現確定何時執行的邏輯(并調用 performSegueWithIdentifier:
來實現).
A manual segue is created in IB by dragging from the source view controller. The upside to this is that a view controller can have multiple outgoing segues. On the other hand, they aren't associated with a tappable control, so you have to implement logic that determines which to perform when (and calls performSegueWithIdentifier:
to make it happen).
考慮到這些權衡,您的問題有兩種可能的解決方案:
Given those tradeoffs, there are two possible solutions to your problem:
使用多個原型表單元格——然后每個單元格都可以有自己的傳出自動轉場.您需要更改表格視圖控制器的
tableView:cellForRowAtIndexPath:
以檢查索引路徑的節號并為dequeueReusableCellWithIdentifier:
選擇適當的標識符,但這可能會使事情變得更多如果您的趨勢和故事單元具有不同的內容,則方便或高效.
Use multiple prototype table cells -- then each can have its own outgoing automatic segue. You'll need to change your table view controller's
tableView:cellForRowAtIndexPath:
to check the index path's section number and choose the appropriate identifier fordequeueReusableCellWithIdentifier:
, but this might make things more convenient or efficient if your trend and story cells have different content anyway.
使用手動轉場.然后你的表視圖控制器可以實現 tableView:didSelectRowAtIndexPath:
來調用 performSegueWithIdentifier:
并根據索引路徑的部分選擇適當的標識符.
Use manual segues. Then your table view controller can implement tableView:didSelectRowAtIndexPath:
to call performSegueWithIdentifier:
with the appropriate identifier chosen based on the index path's section.
無論哪種方式,您的 prepareForSegue:sender:
實現看起來都很好.
Either way, your prepareForSegue:sender:
implementation looks fine.
這篇關于來自表視圖控制器的多個 segue的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!