Xcode Swift 回到之前的viewController (TableViewController)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28992040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xcode Swift Go back to previous viewController (TableViewController)
提问by Marco Schl
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.
在我的 TableviewController 中,右上角有一个条形按钮项。单击按钮后,将显示一个新的视图控制器。在这个控制器中,我在文本字段中输入一些数据并保存它们。要保存这些数据,我必须单击一个按钮,该按钮也是右上角的一个条形按钮项。
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
当我使用以下代码时,应用程序给我一个错误,EXC_Breakpoint: (code=IEXC_386..)
然后它转到视图控制器TeamOverviewController
var vc = self.storyboard?.instantiateViewControllerWithIdentifier("TeamOverviewController") as ViewController
self.navigationController?.popToViewController(vc, animated: true)
回答by Syed Tariq
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 unwindToThisViewControllerThis 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 unwindToThisViewControlleras 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.
- 我使用了一个 TableViewController,右上角有一个名为“Next”的条形按钮。单击 Next 时,它会显示一个 ViewController,您可以在其中放置任何输入控件。
- ViewController 的右上角还有一个“保存”按钮。
- 两个控制器都嵌入在 NavigationControllers 中以方便导航
- 当单击 Save 按钮时,它会在保存您需要保存的任何数据后返回到 TableViewController。
- 在 TableViewController 中,您需要添加一个 IBAction 函数,我将其称为unwindToThisViewController。当您触摸 ViewController 中的“保存”按钮时,您会被定向到这里
- 在 ViewController 中,您需要按住 Ctrl 从 Save 按钮拖动到 Exit 图标(ViewController Storyboard 中的第三个图标)。当你这样做时,你会得到一个名为unwindToThisViewController的下拉选项作为你应该选择的呈现转场。现在您的 ViewController 已连接到 unwind segue 进程。
- 在 ViewController 中,不要为 Save 按钮放置 IBActon。而是将您的保存命令放在函数 prepareForSegue 中。
Here is the code for the 2 controllers:
这是2个控制器的代码:
//
// 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")
}
}
回答by Pavel Gatilov
If you using UINavigationController
you can directly pop to viewController
what you need.
如果您使用,UINavigationController
您可以直接弹出viewController
您需要的内容。
Example: [self.navigationController popToViewController:TableViewController animated:YES];
例子: [self.navigationController popToViewController:TableViewController animated:YES];