![](/img/trans.png)
[英]Programmatically go back to previous ViewController in Swift
[英]Xcode Swift Go back to previous viewController (TableViewController)
在我的TableviewController中,我在右上角有一個酒吧按鈕項。 單擊按鈕后,將顯示一個新的視圖控制器。 在此控制器中,我將一些數據輸入到文本字段中並保存。 要保存此數據,我必須單擊一個按鈕,該按鈕也是右上角的條形按鈕項。
但是現在它將我委派給我的應用程序的第一個視圖控制器。 我該怎么做,將我委派回表視圖控制器?
編輯:
當我使用以下代碼時,應用程序給我一個EXC_Breakpoint: (code=IEXC_386..)
錯誤EXC_Breakpoint: (code=IEXC_386..)
,它進入視圖控制器TeamOverviewController
var vc = self.storyboard?.instantiateViewControllerWithIdentifier("TeamOverviewController") as ViewController
self.navigationController?.popToViewController(vc, animated: true)
看來該解決方案可以使用標准的“解開序列”。 關於此問題有很多文章,但我總結了一種希望與您的問題相似的方法。
這是兩個控制器的代碼:
//
// TableViewController.swift
import UIKit
class TableViewController: UITableViewController {
// add this function. When the detail ViewController is unwound, it executes this
// function
@IBAction func unwindToThisViewController(segue: UIStoryboardSegue) {
println("Returned from detail screen")
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// Return the number of sections.
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// Return the number of rows in the section.
return 3
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
// Configure the cell...
cell.textLabel!.text = "Row: \(indexPath.row)"
return cell
}
}
//
// ViewController.swift
// Detail View
import UIKit
class ViewController: UIViewController {
// When Save button is clicked, view controller is segued to the TableViewController to the function
// unwindToThisViewController
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Insert your save data statements in this function
println("Insert your save data statements in this function")
}
}
如果使用UINavigationController
,則可以直接彈出viewController
。
示例: [self.navigationController popToViewController:TableViewController animated:YES];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.