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