我是iOS新手,目前有3个视图,希望使用滑动手势在它们之间导航。刷卡有效。红色bg视图按我的要求首先加载,而且滑动效果很好,但是点击Next按钮什么也做不了。有人能给我个提示吗?我搞错了,但似乎在谷歌上找不到任何帮助
在我的项目中,我有一个处理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.
}
}
我现在面临的问题是,当下一个按钮被点击在身份证,我想加载会员信息视图,但现在什么也不会发生时,点击它。但是,我在MemberCardViewController的
loadViewController()
方法中设置了断点,当单击按钮时可以看到应用程序正在到达它,但是视图不会更改为Member Info
。 最佳答案
您没有为pvc
的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
的对象)分配给pvc
变量。此外,要使此代码工作,您必须为故事板中的“Id Card”视图控制器分配正确的类。为此,在故事板中选择“Id Card”视图控制器的对象,并在“Identity Inspector”中将其类更改为IdCardViewController
。希望这有帮助!