I am making an app where the user can switch between views using scrollview
, it is like paging with scroll view. I instantiate 3 viewcontrollers
and then put them side by side in the scroll view. The layout and everything are working, the only problem I have, that for some reason I cannot get any button or control to trigger any action/function in the third Viewcontroller.
The first one is just a placeholder, but everything in the second view works, but the third just does not trigger anything. Here is a picture so you can see the setup:
My question is, what can I do so that the segmented control can trigger actions in the third view?
Thanks for your help in advance.
EDIT: Here is some code if it helps!
var appllicationPages: [UIView] {
get {
let firstPage = storyboard!.instantiateViewController(withIdentifier: OnboardPageNames.firstPage).view!
let secondPage = storyboard!.instantiateViewController(withIdentifier: OnboardPageNames.secondPage).view!
let thirdPage = storyboard!.instantiateViewController(withIdentifier: OnboardPageNames.thirdPage).view!
return [firstPage,secondPage,thirdPage]
}
}
func setupScrollView(from pages: [UIView])
{
scrollView.delegate = self
scrollView.isPagingEnabled = true
scrollView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height)
scrollView.contentSize = CGSize(width: view.frame.width * CGFloat(pages.count), height: view.frame.height)
for i in 0 ..< pages.count {
pages[i].frame = CGRect(x: view.frame.width * CGFloat(i), y: 0, width: view.frame.width, height: view.frame.height)
scrollView.addSubview(pages[i])
}
}
And then the function in VC three which is not executing (i hooked the segmented control up with Storyboard)
@IBAction func typeSwitched(_ sender: Any) {
print("Hello There")
}
Thanks to the answers of DonMag and Amais Sheikh i managed to solve the problem!
As described in the comments, the problem was, that i only loaded the views, and not the controllers.
So i just had to add two lines of code:
func setupScrollView(from pages: [UIViewController])
{
scrollView.delegate = self
scrollView.isPagingEnabled = true
scrollView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height)
scrollView.contentSize = CGSize(width: view.frame.width * CGFloat(pages.count), height: view.frame.height)
for i in 0 ..< pages.count {
pages[i].view.frame = CGRect(x: view.frame.width * CGFloat(i), y: 0, width: view.frame.width, height: view.frame.height)
scrollView.addSubview(pages[i].view)
let VC = pages[i]
// These lines helped me solve the problem
self.addChild(VC)
VC.didMove(toParent: self)
}
}
And i changed the array to ViewController instead of Views:
var appllicationPages: [UIViewController] {
get {
let firstPage = storyboard!.instantiateViewController(withIdentifier: MainAppPages.firstPage)
let secondPage = storyboard!.instantiateViewController(withIdentifier: MainAppPages.secondPage)
let thirdPage = storyboard!.instantiateViewController(withIdentifier: MainAppPages.thirdPage)
return [firstPage,secondPage,thirdPage]
}
}