3 回答

TA貢獻2065條經(jīng)驗 獲得超14個贊
control 從View Controller拖動到View Controller
從View Controller到View Controlle!
您需要為您的segue提供一個標識符:
執(zhí)行segue:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:@"yourSegue" sender:self];
}
現(xiàn)在這是事情,如果您需要將一些數(shù)據(jù)傳遞給該視圖控制器,它將僅執(zhí)行segue。然后,您必須實現(xiàn)以下segue委托:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Make sure your segue name in storyboard is the same as this line
if ([[segue identifier] isEqualToString:@"yourSegue"])
{
//if you need to pass data to the next controller do it here
}
}

TA貢獻1848條經(jīng)驗 獲得超10個贊
迅速
該答案顯示了如何傳遞數(shù)據(jù),并針對Xcode 8和Swift 3進行了更新。
這是設置項目的方法。
使用創(chuàng)建一個項目TableView。請參閱此處的簡單示例。
添加第二個ViewController。
從具有表格視圖的第一視圖控制器到第二視圖控制器的控制拖動。選擇“顯示”作為segue類型。
單擊情節(jié)提要中的segue,然后在“屬性檢查器”中,將標識符命名為“ yourSegue”。(您可以隨意調(diào)用它,但還需要在代碼中使用相同的名稱。)
(可選)您可以通過在情節(jié)提要中選擇第一個視圖控制器,然后轉(zhuǎn)到編輯器>嵌入>導航控制器,將所有內(nèi)容嵌入到導航控制器中。
碼
具有TableView的First View控制器:
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
// ...
// method to run when table view cell is tapped
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// Segue to the second view controller
self.performSegue(withIdentifier: "yourSegue", sender: self)
}
// This function is called before the segue
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// get a reference to the second view controller
let secondViewController = segue.destination as! SecondViewController
// set a variable in the second view controller with the data to pass
secondViewController.receivedData = "hello"
}
}
第二視圖控制器
class SecondViewController: UIViewController {
@IBOutlet weak var label: UILabel!
// This variable will hold the data being passed from the First View Controller
var receivedData = ""
override func viewDidLoad() {
super.viewDidLoad()
print(receivedData)
}
}
有關(guān)在視圖控制器之間傳遞數(shù)據(jù)的更基本的示例

TA貢獻1796條經(jīng)驗 獲得超10個贊
這是另一個選項,不需要使用didSelectRowAtIndexPath。
您只需將Interface Builder中的序列從原型單元連接到目標即可。
從那里您可以簡單地:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "AffiliationDetail", let destination = segue.destinationViewController as? AffiliateDetailViewController {
if let cell = sender as? UITableViewCell, let indexPath = tableView.indexPathForCell(cell) {
var affiliation = affiliations[indexPath.row]
destination.affiliation = affiliation
}
}
}
- 3 回答
- 0 關(guān)注
- 533 瀏覽
添加回答
舉報