【问题标题】:UIButton Inside Nested ScrollViews is Highlighting but not Triggering Segue or IBAction When Tapped - Swift嵌套 ScrollViews 内的 UIButton 突出显示,但在点击时不会触发 Segue 或 IBAction - Swift
【发布时间】:2016-06-17 16:52:41
【问题描述】:

我有一个 UIButton 位于嵌套滚动视图中(水平分页滚动视图中的垂直分页滚动视图)。我的问题是当我点击它突出显示的按钮时,它不会触发我链接到的 segue。然后,我尝试将其链接到放置断点的 IBAction 函数,当我点击按钮时,我没有击中该断点,因此我的按钮也没有触发 IBActions。

我尝试关闭两个滚动视图的延迟内容触摸,但没有奏效。所做的只是在我点击按钮后立即突出显示它,而不是在突出显示之前稍微增加一点权重。无论哪种方式,当我点击它时,我的按钮都会突出显示,但我的所有插座(segues 或 IBActions)都没有触发,我认为这很奇怪。我在网上发现其他人有类似的问题,但他们的按钮甚至没有突出显示。我的亮点,所以我知道它从用户那里得到输入。

这是我实例化嵌套滚动视图的方式。主水平滚动视图是在我的故事板中创建的,然后在它的 ViewController viewDidLoad 方法中我这样做是为了嵌套垂直滚动视图。垂直滚动视图包含我的按钮。

let readingVC = storyboard!.instantiateViewControllerWithIdentifier("ReadingViewController") as! ReadingRingViewController
readingVC.read = reading.minutesRead
readingVC.goal = reading.minutesGoal
readingVC.selectedDate = selectedDate
readingVC.today = today
let readingView = readingVC.view
let scrollPage2 = UIScrollView(frame: CGRectMake(readingView.bounds.size.width,0,readingView.bounds.size.width, readingView.bounds.size.height))
scrollPage2.addSubview(readingView)
scrollPage2.contentSize = CGSize(width: readingView.bounds.size.width, height: readingView.bounds.size.height*2)
scrollPage2.pagingEnabled = true;
scrollPage2.alwaysBounceVertical = true
scrollPage2.userInteractionEnabled = true
let readingBottomView = storyboard!.instantiateViewControllerWithIdentifier("ReadingBottomViewController").view
let scrollPage2Bottom = UIScrollView(frame: CGRectMake(0,readingView.bounds.size.height,readingBottomView.bounds.size.width, readingBottomView.bounds.size.height*2))
scrollPage2Bottom.addSubview(readingBottomView)
        scrollPage2Bottom.contentSize = CGSize(width: readingBottomView.bounds.size.width, height: readingBottomView.bounds.size.height)
scrollPage2.addSubview(scrollPage2Bottom)
scrollPage2.sendSubviewToBack(scrollPage2Bottom)

这是我的按钮所在的 viewController 的代码。我已将其链接到 IBAction 并将其链接到另一个 viewController 作为推送序列

@IBAction func start(sender: AnyObject) {
    var x = 0
    var y = 0
    x = y + x
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "StartReading" {
        let destVC = segue.destinationViewController
    }
}

【问题讨论】:

    标签: ios iphone swift uiscrollview uibutton


    【解决方案1】:

    我解决了我自己的问题。按钮的操作不起作用的原因是因为我将按钮所在的视图添加为子视图,但忘记将按钮视图的视图控制器也添加为子视图控制器。所以我在实例化 readingVC 并设置了它的成员变量后,在代码行后面插入了几行:

    self.addChildViewController(readingVC)
    readingVC.didMoveToParentViewController(self)
    

    现在按钮完全按预期工作了

    【讨论】:

    • 它就像一个魅力!十分感谢!!!在 Objective-C 中:[self addChildViewController:viewController]; [viewController didMoveToParentViewController:self]; [scrollView addSubview:viewController.view];
    猜你喜欢
    • 2019-11-30
    • 2014-03-10
    • 1970-01-01
    • 2017-05-20
    • 2014-02-03
    • 1970-01-01
    • 2014-02-19
    • 2013-07-05
    • 1970-01-01
    相关资源
    最近更新 更多