从滚动视图中的视图触发动作
Trigger action from view within scrollview
我正在制作一个应用程序,用户可以使用 scrollview
在视图之间切换,就像使用滚动视图进行分页一样。我实例化 3 viewcontrollers
然后将它们并排放在滚动视图中。布局和一切正常,我遇到的唯一问题是,由于某种原因,我无法获得任何按钮或控件来触发第三个 Viewcontroller.
中的任何 action/function
第一个只是一个占位符,但第二个视图中的所有内容都有效,但第三个视图不会触发任何内容。这是一张图片,您可以看到设置:
我的问题是,我该怎么做才能让分段控件在第三视图中触发动作?
提前感谢您的帮助。
编辑:如果有帮助,这里是一些代码!
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])
}
}
然后 VC 三个中的函数未执行(我将分段控件与 Storyboard 挂钩)
@IBAction func typeSwitched(_ sender: Any) {
print("Hello There")
}
感谢 DonMag 和 Amais Sheikh 的解答,我成功解决了问题!
如评论中所述,问题是我只加载了视图,而不是控制器。
所以我只需要添加两行代码:
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)
}
}
并且我将数组更改为 ViewController 而不是视图:
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]
}
}
我正在制作一个应用程序,用户可以使用 scrollview
在视图之间切换,就像使用滚动视图进行分页一样。我实例化 3 viewcontrollers
然后将它们并排放在滚动视图中。布局和一切正常,我遇到的唯一问题是,由于某种原因,我无法获得任何按钮或控件来触发第三个 Viewcontroller.
第一个只是一个占位符,但第二个视图中的所有内容都有效,但第三个视图不会触发任何内容。这是一张图片,您可以看到设置:
我的问题是,我该怎么做才能让分段控件在第三视图中触发动作?
提前感谢您的帮助。
编辑:如果有帮助,这里是一些代码!
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])
}
}
然后 VC 三个中的函数未执行(我将分段控件与 Storyboard 挂钩)
@IBAction func typeSwitched(_ sender: Any) {
print("Hello There")
}
感谢 DonMag 和 Amais Sheikh 的解答,我成功解决了问题!
如评论中所述,问题是我只加载了视图,而不是控制器。
所以我只需要添加两行代码:
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)
}
}
并且我将数组更改为 ViewController 而不是视图:
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]
}
}