从入职/教程屏幕执行segue

时间:2017-01-21 23:35:14

标签: ios swift xcode swift3 uipageviewcontroller

我正在使用UIPageViewController进行登机/教程屏幕,我的问题如下:

当用户在教程的最后一页上时,我想要一个左轻扫手势,不要尝试滑动页面控制器,而是执行segue到注册页面。我在本教程的最后一页的viewDidLoad中的UIViewController中输入了一个print语句,然后进行了注册。

在同一个viewDidLoad我创建我的手势来执行segue,但没有任何反应。我甚至尝试将performSegue放在viewDidLoad中,但什么都没发生。我将我的segue连接到UIPageViewController以及GrowController,并且都没有注册。我很茫然!

这是教程最后一页的UIViewController

class GrowController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    print("*********************************")
    createGesture()
    // performSegue(withIdentifier: "ShowRegister", sender: self)
}


func createGesture() {
    let showReg = UISwipeGestureRecognizer(target: self, action: #selector(showRegister))
    showReg.direction = .left
    view.addGestureRecognizer(showReg)
}


func showRegister(gesture: UISwipeGestureRecognizer) {
    performSegue(withIdentifier: "showRegister", sender: self)
}

}

这是处理教程逻辑的UIPageViewController

class TutorialViewController: UIPageViewController, UIPageViewControllerDelegate, UIPageViewControllerDataSource {

    var viewControllerIndex: Int?

lazy var tutorialArray: [UIViewController] = {
    return [self.tutorialInstance(name: "page1"), self.tutorialInstance(name: "page2"), self.tutorialInstance(name: "page3")]
}()


private func tutorialInstance(name: String?) -> UIViewController {
    return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: name!)
}



override func viewDidLoad() {
    super.viewDidLoad()
    if let image = UIImage(named: "export.png") {
        view.backgroundColor = UIColor.init(patternImage: image)
    } else {
        print("Error")
    }

    self.dataSource = self
    self.delegate = self

    if let firstViewController = tutorialArray.first {
        setViewControllers([firstViewController], direction: .forward, animated: false, completion: nil)
    }
}

这些是delgate和数据源函数

// Page View Controller delegate functions
public func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
    guard let viewControllerIndex = tutorialArray.index(of: viewController) else {
        return nil
    }

    let previousIndex = viewControllerIndex - 1

    guard previousIndex >= 0 else {
            return nil
    }

    guard tutorialArray.count > previousIndex else {
        performSegue(withIdentifier: "ShowRegister", sender: self)  // Added this line just testng around, nothing happend here though.
        return nil
    }

    return tutorialArray[previousIndex]
}


public func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
    guard let viewControllerIndex = tutorialArray.index(of: viewController) else {
        return nil
    }

    let nextIndex = viewControllerIndex + 1

    guard nextIndex < tutorialArray.count else {
        return nil
    }

    guard tutorialArray.count > nextIndex else {
        return nil
    }

    return tutorialArray[nextIndex]
}

public func presentationCount(for pageViewController: UIPageViewController) -> Int {
    return tutorialArray.count
}

public func presentationIndex(for pageViewController: UIPageViewController) -> Int {
    guard let firstViewController = viewControllers?.first, let firstViewControllerIndex = tutorialArray.index(of: firstViewController) else {
        return 0
    }
    return firstViewControllerIndex
}

很多代码可能是一个简单的答案。

tl; dr - 如何从UIPageViewController

的特定页面执行滑动手势执行segue

1 个答案:

答案 0 :(得分:0)

有一种代理方法可以实施pageViewController(_:willTransitionTo:),但我不知道它值得一试。也许只是在它前面放置另一个视图并在该视图中放置一个滑动手势识别器视图,并使用识别的手势手动更改页面或segue到新控制器。