问题
In my TableviewController I have a bar button item in the top right corner. After clicking the button a new view controller is shown. In this Controller I enter some data in to textfields and save them. To save this data I have to click on a button, which is also a bar button item in the right corner.
But now it delegates me to the first view Controller of my app. What did I do, that it delegates me back to the table view controller?
EDIT:
When I use the following code, the app give me an error of EXC_Breakpoint: (code=IEXC_386..)
and the it goes to the view controller TeamOverviewController
var vc = self.storyboard?.instantiateViewControllerWithIdentifier("TeamOverviewController") as ViewController
self.navigationController?.popToViewController(vc, animated: true)
回答1:
It appears that the solution could use a standard "unwind segue". There are a number of posts on this issue but I have summarized an approach which hopefully resembles your problem.
- I use a TableViewController with a bar button in the top right corner called "Next". When Next is clicked it displays a ViewController in which you can put any input controls.
- The ViewController also has a "Save" button in the top right corner.
- Both controllers are embedded in NavigationControllers to facilitate navigation
- When the Save button is clicked, it returns to the TableViewController after saving whatever data you need to save.
- In the TableViewController you need to add an IBAction function which I called unwindToThisViewController This is where you are directed to when you touch the Save button in ViewController
- In ViewController you need to Ctrl-Drag from the Save button to the Exit icon (the third icon in the ViewController Storyboard). When you do that you will get a dropdown option with the name unwindToThisViewController as the presenting segue which you should select. Now your ViewController is connected to the unwind segue process.
- In ViewController, do not put an IBActon for the Save button. Instead put your save commands in the function prepareForSegue.
Here is the code for the 2 controllers:
//
// 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")
}
}
回答2:
If you using UINavigationController
you can directly pop to viewController
what you need.
Example: [self.navigationController popToViewController:TableViewController animated:YES];
来源:https://stackoverflow.com/questions/28992040/xcode-swift-go-back-to-previous-viewcontroller-tableviewcontroller