【问题标题】:Swift - SwipingController Programmatic - button doesn't workSwift - SwipingController Programmatic - 按钮不起作用
【发布时间】:2020-12-20 08:48:17
【问题描述】:

我创建了 SwipingController 应用程序。 该应用程序应该具有手势滚动功能和一个带有 2 个按钮和 UIPageControl 的管理栏。

目前,这些按钮本应仅在控制台中打印一条文本消息,但事实并非如此。

 let nextButton: UIButton = {
    let button = UIButton(type: .system)
    button.setTitle("NEXT", for: .normal)
    button.setTitleColor(.black, for: .normal)
    button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14)
    button.addTarget(self, action: #selector(handleNextButton), for: .touchUpInside)

    return button
}()
@objc func handleNextButton() {
    print("Next Botton Pressed")
}

我想将整个页面管理栏添加到一个单独的文件中。

当它进入主控制器时,整个功能都会起作用。

我不想粘贴所有代码,所以它给出了 git 的链接

https://github.com/SebaKrk/SwipingControllerProgrammatic.git

Picture from simulator

【问题讨论】:

  • 我看过你的项目。您的按钮位于 UICollectionViewCell 中。在使用单元格时,您应该考虑使用委托模式来处理操作。
  • 好的,我将实现委托模式并传递到这里检查

标签: ios swift programmatic-config


【解决方案1】:

问题是您在UIButton 的设置代码中正确设置了目标

let previousBotton : UIButton = {
        let button = UIButton(type: .system)
        button.setTitle("PREV", for: .normal)
        button.setTitleColor(.black, for: .normal)
        button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14)
        button.addTarget(self, action: #selector(handlePreviousButton), for: .touchUpInside)
        return button
    }()

此时self 似乎尚未初始化。因为这段代码是在你的init 运行之前运行的。

所以你必须在调用super.init 之后设置按钮的target 才能工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-05
    • 2017-08-29
    相关资源
    最近更新 更多