Xcode Swift 回到之前的viewController (TableViewController) [英] Xcode Swift Go back to previous viewController (TableViewController)
问题描述
在我的 TableviewController 中,我的右上角有一个条形按钮项.单击按钮后,将显示一个新的视图控制器.在这个控制器中,我将一些数据输入到文本字段中并保存它们.要保存这些数据,我必须单击一个按钮,该按钮也是右上角的一个条形按钮项.
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?
当我使用以下代码时,应用程序给我一个 EXC_Breakpoint: (code=IEXC_386..)
的错误,然后它转到视图控制器 TeamOverviewController
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)
推荐答案
看来该解决方案可以使用标准的展开转场".关于这个问题有很多帖子,但我总结了一种方法,希望与您的问题类似.
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.
- 我使用了一个 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 中.
- 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")
}
}
这篇关于Xcode Swift 回到之前的viewController (TableViewController)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!