返回上一个(根)ViewController

Returning to previous (root) ViewController

我在 TableView 中有一个产品列表。通过点击一个产品,我打开了 UIPageViewController,它包含类似的产品,用户可以在它们之间滑动。我需要做的是,当用户在第一个项目上并且如果他向后滑动时,UIPageViewController 必须消失并且用户应该回到 TableView 上。默认滑动手势已启用(当您从边缘滑动返回但它不适合我的需要时)。原理应该是一样的,只是不需要从边缘滑动,但它应该像常规滑动一样工作。你们能帮忙出出主意吗?

您可以使用

self.navigationController?.popViewControllerAnimated(true);

您想要一个 "back" 滑动按钮。 因此,您需要添加滑动手势识别器,然后关闭视图。

在viewDidLoad()中

var swipe = UISwipeGestureRecognizer(target: self, action: Selector("swipes:"))
swipe.direction = .Right
view.addGestureRecognizer(swipe)

并在处理函数中。

func swipes(sender:UISwipeGestureRecognizer) {
 if (sender.direction == .Right) {
     self.navigationController?.popViewControllerAnimated(true); //If you are now using navigation controller, just change to the apropriate function to "go back"
}
}