我尝试使用此工作流程构建iOS应用程序:在这种情况下,用户会看到一个主视图ViewController,然后可以单击UITableViewCell,以便通过自定义segue定向到DetailCountryViewController( SegueRight与标识符doneSegueToCountryDetail)。这非常有效。然后,当用户向右滑动(使用Swipe Gesture Recognizer)时,它应该展开并将其发送回ViewController。另外,使用自定义segue,SegueLeft使用标识符segueCountryDetailToMain)。 但是,这会冻结应用程序(在ViewController已经显示之后,这样可行),几秒钟后它会崩溃,代码为EXC_BAD_ACCESS。到目前为止,这是我的代码:
Swift 3
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
public func tableView(_ tableView: UITableView, didSelectRowAt
indexPath: IndexPath){
let indexPath = tableView.indexPathForSelectedRow
let currentCell = tableView.cellForRow(at: indexPath!) as UITableViewCell!
tableView.deselectRow(at: indexPath!, animated: true)
titleToPass = currentCell?.textLabel?.text?.capitalized
if tableView == self.tableViewDone {
performSegue(withIdentifier: "doneSegueToCountryDetail", sender: self)
} else if tableView == self.tableViewWish {
performSegue(withIdentifier: "wishSegueToCountryDetail", sender: self)
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "doneSegueToCountryDetail" || segue.identifier == "wishSegueToCountryDetail" {
if let exitVC = segue.destination as? DetailCountryViewController {
exitVC.titleVar = titleToPass
}
}
}
@IBAction func unwindToMainContent(segue:UIStoryboardSegue) {
}
}
class SegueFromLeft: UIStoryboardSegue
{
override func perform()
{
print("SegueFromLeft called")
let src = self.source
let dst = self.destination
src.view.superview?.insertSubview(dst.view, aboveSubview: src.view)
dst.view.transform = CGAffineTransform(translationX: src.view.frame.size.width, y: 0)
UIView.animate(withDuration: 0.25,
delay: 0.0,
options: UIViewAnimationOptions.curveEaseInOut,
animations: {
dst.view.transform = CGAffineTransform(translationX: 0, y: 0)
},
completion: { finished in
src.present(dst, animated: false, completion: nil)
}
)
}
}
class DetailCountryViewController: UIViewController {
var titleVar = ""
@IBAction func swipeBack(_ sender: UISwipeGestureRecognizer) {
performSegue(withIdentifier: "segueCountryDetailToMain", sender: self)
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
它在SegueLeft中的perform()函数崩溃
completion: { finished in
src.present(dst, animated: false, completion: nil) }
我似乎无法找到错误。崩溃时,src变量设置为DetailCountryViewController,dst设置为ViewController。错误在哪里?
更新1 所以,我已经修好了#39;这可以通过将发生崩溃的上述行更改为
completion: nil
因此,就我从文档中理解的那样,成功显示ViewController时没有调用任何内容。但是,当我现在再次返回DetailCountryViewController时,它再次崩溃。这一次,因为DetailCountryViewController已经在ViewController中呈现,我试图再次呈现它(当然)。 因此我现在知道,我选择的完成处理程序src.present可能不正确或者我应该使用其他东西。但是什么?我是否必须在任何地方解雇任何控制器,或者......?