問題描述
我希望我問的不是已經回答的問題(但我沒有找到答案,所以希望我沒有).
I hope I'm not asking something that's been already answered (but I found no answer to this, so hopefully I'm not).
我在當前的 xcode 版本中有一個應用程序,使用 segues 和 navigationController.我需要將數據從一個視圖傳遞到另一個視圖 - 最簡單的方法是什么?我遇到了一些可能與 performSegueWithIdentifier 方法掛鉤的 sharedData 東西,但不知道如何使用它(或者這樣做是否是正確的選擇).
I have an app in the current xcode version, using segues and navigationController. I need to pass data from one view to the other - what's the easiest way to do this? I ran onto some sharedData thing that could be possibly hooked onto the performSegueWithIdentifier method but don't know how to use it (or whether it is the right choice to do it like this).
謝謝
推薦答案
一個segue有兩個視圖控制器:sourceViewController
和destinationViewController
.當 UIKit 執行 segue 時,它??會向源 VC 發送 prepareForSegue:sender:
消息.您可以在視圖控制器子類中重寫該方法以將數據傳遞給目標 VC.
A segue has two view controllers: sourceViewController
and destinationViewController
. When UIKit executes a segue, it sends a prepareForSegue:sender:
message to the source VC. You can override that method in your view controller subclass to pass data to the destination VC.
例如,假設您有一個帶有電影表視圖的主視圖控制器,并且當用戶單擊表視圖中的一行時,您希望轉到電影的詳細視圖控制器.
For example, suppose you have a master view controller with a table view of movies, and when the user clicks a row in the table view, you want to segue to a detail view controller for the movie.
@implementation MasterViewController
...
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
DetailViewController *detailVC = segue.destinationViewController;
NSIndexPath *selectedPath = [self.tableView indexPathForSelectedRow];
detailVC.movie = [self movieForIndexPath:selectedPath];
}
Interface Builder Storyboard 簡介中對此進行了解釋來自 WWDC 2011 的視頻.
This is explained in the Introducing Interface Builder Storyboarding video from WWDC 2011.
另外值得注意的是,當segue的來源是table view cell,或者table view cell的附屬按鈕時,prepareForSegue:sender:
的sender
參數是表格視圖單元格.
It's also worth noting that when the segue's origin is a table view cell, or the accessory button of a table view cell, the sender
argument of prepareForSegue:sender:
is the table view cell.
這篇關于performSegueWithIdentifier 和 sharedData的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!