簡體   English   中英

Swift - SwipingController 編程 - 按鈕不起作用

[英]Swift - SwipingController Programmatic - button doesn't work

我創建了 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")
}

我想將整個頁面管理欄添加到一個單獨的文件中。

當它轉到主要的 controller 時,整個功能都可以工作。

我不想粘貼所有代碼,所以它提供了 git 的鏈接

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

圖片來自模擬器

問題是您在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之后設置 Button 的target ,然后它才能工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM