简体   繁体   English

UIButton无法在子ViewController中加载

[英]UIButton not working loaded in a child ViewController

I've got a MainViewController where I'm loading a slideout-menu. 我有一个MainViewController ,我正在加载一个slideout-menu。 The MainViewController contains a static TopView and a ContentView where different child controllers or views are loaded, depending on which menu entry was selected. MainViewController包含一个静态TopView和一个ContentView ,其中加载了不同的子控制器或视图,具体取决于所选的菜单项。

There is a button inside one of the children is loaded, but it is not working. 其中一个孩子内部有一个按钮被加载,但它无法正常工作。 When I start the app with the ViewController including the button, set to "is initial View Controller", everything works properly. 当我使用包含按钮的ViewController启动应用程序时,设置为“是初始视图控制器”,一切正常。

When starting the app with the MainViewController as initial View Controller with a loaded child, the button is not working (it is loaded in the view, but not reacting). 当使用MainViewController作为具有加载子项的初始View Controller启动应用程序时,该按钮不起作用(它在视图中加载,但没有响应)。 Any suggestions how I can make this work? 有什么建议我可以做这个工作吗?

MainViewController loading of Child View 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 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)
    }
}

Update your functions to these 更新您的功能

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)
}

And change your code of adding the target to 并更改添加目标的代码

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

While adding the view in the container you don't have to use addSubview function, that the FirstViewController functionality is not working. 在容器中添加视图时,您不必使用addSubview函数,FirstViewController功能无法正常工作。

Use below code: 使用以下代码:

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)
    })

You can change the duration, it used for animation 您可以更改用于动画的持续时间

First solution: 第一解决方案

Remove an autolayout constrains 删除自动布局约束

Second solution: 二解决方案:

Button is not performing an action because a child view controller view should be added directly to a container controller view (in your case: MainViewController view) and not via an intermediate view (in your case: contentView ) Button没有执行操作,因为子视图控制器视图应该直接添加到容器控制器视图(在您的情况下: MainViewController视图)而不是通过中间视图(在您的情况下: contentView

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM