簡體   English   中英

UIButton無法在子ViewController中加載

[英]UIButton not working loaded in a child ViewController

我有一個MainViewController ,我正在加載一個slideout-menu。 MainViewController包含一個靜態TopView和一個ContentView ,其中加載了不同的子控制器或視圖,具體取決於所選的菜單項。

其中一個孩子內部有一個按鈕被加載,但它無法正常工作。 當我使用包含按鈕的ViewController啟動應用程序時,設置為“是初始視圖控制器”,一切正常。

當使用MainViewController作為具有加載子項的初始View Controller啟動應用程序時,該按鈕不起作用(它在視圖中加載,但沒有響應)。 有什么建議我可以做這個工作嗎?

MainViewController加載Child View

class MainViewController: UIViewController, SideBarDelegate {
var contentView = UIView()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    ....
    contentView.backgroundColor = UIColor(red: 70/255, green: 174/255, blue: 253/255, alpha: 1)
    contentView.translatesAutoresizingMaskIntoConstraints = false
    self.view.addSubview(contentView)
    contentView.topAnchor.constraint(equalTo: statusBarView.bottomAnchor).isActive = true
    contentView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
    contentView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
    contentView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
    contentView.layoutIfNeeded()

}

func sideBarDidSelectButtonAtIndex(_ index: Int) {
    ...       
    if index == 0{
        statusBarLabel.text = "First"
        let controller:FirstViewController = storyboard!.instantiateViewController(withIdentifier: "First") as! FirstViewController
        controller.view.frame = ContentView.bounds
        controller.willMove(toParentViewController: self)
        contentView.addSubview(controller.view)
        controller.didMove(toParentViewController: self)
        print("touched index 0")
    } else if index == 1{
        // index is 1
    }
}

FirstViewController

class FirstViewController: UIViewController, UIScrollViewDelegate {

    let addButton = UIButton()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = UIColor(red: 230/255, green: 230/255, blue: 230/255, alpha: 1)

        addButton.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 60)
        addButton.clipsToBounds = true
        addButton.setTitleColor(UIColor.white, for: .normal)
        addButton.backgroundColor = UIColor(red: 102/255, green: 204/255, blue: 255/255, alpha: 1)
        addButton.setTitle("add feed", for: .normal)
        addButton.titleLabel?.font = UIFont.systemFont(ofSize: 20, weight: UIFontWeightRegular)
        self.view.addSubview(AddButton)
        addButton.layoutIfNeeded()
        addButton.addTarget(self, action: #selector(touchUpAddButton), for: UIControlEvents.touchUpInside)
        addButton.addTarget(self, action: #selector(touchDownAddButton), for: UIControlEvents.touchDown)

    func touchUpAddButton() {
        addButton.backgroundColor = UIColor(red: 102/255, green: 204/255, blue: 255/255, alpha: 1)
    }

    func touchDownAddButton() {
        addButton.backgroundColor = UIColor(red: 70/255, green: 174/255, blue: 253/255, alpha: 1)
    }
}

更新您的功能

func TouchUpAddButton(sender: UIButton){
    addButton.backgroundColor = UIColor(red: 102/255, green: 204/255, blue: 255/255, alpha: 1)

}

func TouchDownAddButton(sender: UIButton) {
    addButton.backgroundColor = UIColor(red: 70/255, green: 174/255, blue: 253/255, alpha: 1)
}

並更改添加目標的代碼

addButton.addTarget(self, action: #selector(FirstViewController.touchUpAddButton(_:)), for: .touchDown)
addButton.addTarget(self, action: #selector(FirstViewController.touchDownAddButton(_:)), for: .touchDown)

在容器中添加視圖時,您不必使用addSubview函數,FirstViewController功能無法正常工作。

使用以下代碼:

let newViewController:FirstViewController = storyboard!.instantiateViewController(withIdentifier: "First") as! FirstViewController
let oldViewController  = childViewControllers.last! as UIViewController
    oldViewController.willMove(toParentViewController: nil)
    addChildViewController(newViewController)
    transition(from: oldViewController, to: newViewController, duration: 0.25, options: .transitionCrossDissolve, animations:{ () -> Void in
        // nothing needed here
    }, completion: { (finished) -> Void in
        oldViewController.removeFromParentViewController()
        newViewController.didMove(toParentViewController: self)
    })

您可以更改用於動畫的持續時間

第一解決方案

刪除自動布局約束

二解決方案:

Button沒有執行操作,因為子視圖控制器視圖應該直接添加到容器控制器視圖(在您的情況下: MainViewController視圖)而不是通過中間視圖(在您的情況下: contentView

暫無
暫無

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

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