Xcode Swift Go back to previous viewController (TableViewController)

耗尽温柔 提交于 2020-01-05 13:08:36

问题


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.

  1. 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.
  2. The ViewController also has a "Save" button in the top right corner.
  3. Both controllers are embedded in NavigationControllers to facilitate navigation
  4. When the Save button is clicked, it returns to the TableViewController after saving whatever data you need to save.
  5. 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
  6. 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.
  7. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!