【问题标题】:Trigger action from view within scrollview从滚动视图中的视图触发操作
【发布时间】:2021-07-26 03:59:57
【问题描述】:

我正在制作一个应用程序,用户可以使用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 3 中未执行的函数(我将分段控件与 Storyboard 挂钩)

@IBAction func typeSwitched(_ sender: Any) {
    print("Hello There")
}

【问题讨论】:

  • 从你的解释中,我知道你只想在第三个视图控制器中触发一个动作,而不是在放置滚动视图的类中。考虑到这一点,您似乎没有正确触发第三个视图控制器类中所需的操作。如果您希望在放置滚动视图的类中触发操作,那么您必须在滚动视图容器类和第三个视图控制器之间使用某种通信模式(例如委托模式)才能使操作正常工作。
  • 如果还是不行,可以显示代码!
  • @AmaisSheikh 感谢您的回复,是的,我希望该操作仅在第三个 VC 中发生。我刚刚使用故事板制作了一个 ibaction(这将是动作),但我为第二个 VC 中的按钮以相同的方式制作了它,它在那里发挥了应有的作用。
  • @ElliotCzigány - 这是您使用的实际代码吗?您展示的是从 3 个故事板视图控制器中的每一个加载 视图,但您没有从这些视图控制器加载任何 代码
  • 不确定这是否是正确的方法。如果您希望视图具有适当的功能,则必须研究子视图控制器的概念,实例化视图控制器并将其视图添加到包含视图的子视图控制器。其余的由@DonMag 指定

标签: ios swift xcode uiscrollview


【解决方案1】:

感谢 DonMag 和 Amais Sheikh 的回答,我设法解决了这个问题!

如 cmets 中所述,问题在于,我只加载了视图,而不是控制器。

所以我只需要添加两行代码:

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 而不是 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]
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-24
    • 1970-01-01
    • 2019-07-04
    • 1970-01-01
    • 2018-10-29
    • 2012-11-03
    • 1970-01-01
    • 2021-09-26
    相关资源
    最近更新 更多