I am new to iOS and currently have 3 views and want to use the swipe gesture to navigate between them. Swiping works. The red bg view loads first as I want it to, and swiping works well, however tapping the Next button isnt doing anything. Could anyone please give me a hint? I am approaching this wrong, but cant seem to find any help on google
In my project I have a MemberCardViewController that handles the UIPageViewController:
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.
}
}
...
And for now, I also created a IdCardViewController that corresponds to the Id Card (red background view)
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.
}
}
The issue I am facing now is that when Next button is clicked in Id Card, I want to load the Member Info view, but right now nothing happens when its clicked. However, i set up breakpoints in MemberCardViewController's loadViewController()
method and can see that the app is reaching it when the button is clicked, however the view doesnt change to Member Info
.
You are not assigning anything to the pvc
variable of IdCardViewController
. So, when you instantiate the instance of IdCardViewController
, you should assign MemberCardViewController
object to pvc
variable. Good Luck!
EDIT
Change
private func ViewControllerInstance(name: String) -> UIViewController {
return UIStoryboard(name: "MemberCardViewController", bundle: nil).instantiateViewController(withIdentifier: name)
}
to
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)
}
What is done here is, after instantiating the instance of IdCardViewController
, an object of MemberCardViewController
(which is self
in this case) is being assigned to pvc
variable. Also, for this code to work, you have to assign right class for the "Id Card" view controller in storyboard. To do so, select "Id Card" view controller's object in storyboard, and change it's class to IdCardViewController
in "Identity Inspector".
Hope this helps!