繁体   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