带子ViewControllers

时间:2017-09-19 09:28:46

标签: ios swift uipageviewcontroller

我是iOS新手,目前有3个视图,想要使用滑动手势在它们之间导航。刷卡工作。红色bg视图首先按照我想要的方式加载,并且滑动效果很好,但是点击“下一步”按钮不会做任何事情。有人可以给我一个提示吗?我正在接近这个错误,但似乎无法在谷歌找到任何帮助

project

在我的项目中,我有一个处理UIPageViewController的MemberCardViewController:

class MemberCardViewController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {

    lazy var viewArray: [UIViewController] = {

        return [self.ViewControllerInstance(name: "CalendarView"),
                self.ViewControllerInstance(name: "IdCardView"),
                self.ViewControllerInstance(name: "MemberInfoView")]
    }()

    private func ViewControllerInstance(name: String) -> UIViewController {
        return UIStoryboard(name: "MemberCardViewController", bundle: nil).instantiateViewController(withIdentifier: name)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        self.dataSource = self
        self.delegate = self
        loadViewController(viewPosition: 1)
    }

    func loadViewController(viewPosition: Int) {
        if let viewController = viewArray.get(at: viewPosition) {
            setViewControllers([viewController], direction: .forward, animated: true, completion: nil)
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}
...

目前,我还创建了一个与Id卡相对应的IdCardViewController(红色背景视图)

class IdCardViewController: UIViewController {

    var pvc = MemberCardViewController()

    @IBAction func loadRight(_ sender: Any) {
        pvc.loadViewController(viewPosition: 2)
    }
    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.
    }
}

我现在面临的问题是,当在Id卡中单击“下一步”按钮时,我想加载“成员信息”视图,但现在点击它时没有任何反应。但是,我在MemberCardViewController的loadViewController()方法中设置断点,并且可以看到应用在单击按钮时到达它,但视图不会更改为Member Info

1 个答案:

答案 0 :(得分:1)

您没有为pvc的{​​{1}}变量分配任何内容。因此,当您实例化IdCardViewController的实例时,您应该将IdCardViewController对象分配给MemberCardViewController变量。祝你好运!

修改

更改

pvc

private func ViewControllerInstance(name: String) -> UIViewController {
        return UIStoryboard(name: "MemberCardViewController", bundle: nil).instantiateViewController(withIdentifier: name)
    }

这里做的是,在实例化private func ViewControllerInstance(name: String) -> UIViewController { if name == "IdCardView" { if let vc = UIStoryboard(name: "MemberCardViewController", bundle: nil).instantiateViewController(withIdentifier: name) as? IdCardViewController { vc.pvc = self return vc } } return UIStoryboard(name: "MemberCardViewController", bundle: nil).instantiateViewController(withIdentifier: name) } 的实例后,IdCardViewController的对象(在这种情况下为MemberCardViewController)被分配给self变量。此外,要使此代码起作用,您必须为" Id卡"分配正确的类。在故事板中查看控制器。为此,请选择" Id Card"在故事板中查看控制器的对象,并在" Identity Inspector"中将其更改为pvc。 希望这有帮助!