从swift 3中的另一个ViewController更改ViewController的UIButton

时间:2017-08-27 16:30:28

标签: ios swift uiviewcontroller uibutton

我有两个ViewController说A,B和C A包含UIBUtton和一个函数的数组。该功能使用标签来识别按下的按钮,有一个按钮(红色x按钮),用菜单显示A的A ontop,当点击菜单(即B)时,它会在导航/推送到视图之前移除控制器B.控制器C.这很好用。

单击时,控制器A中的红色按钮从“x”旋转到“+”。如果“+”,则显示控制器B,如果“x”被移除。问题是当我在C上并且我返回时按钮仍然保持为“+”而不是“x”默认状态..我已经尝试了很多来自SO的解决方案,但没有人倾向于解决我的问题。下面是截至目前为止的截图和代码

Screenshot of ViewController B ontop of A

查看控制器A

@IBAction func didPressTab(_ sender: UIButton) {
        if (sender.tag != 2) {
            let previousIndex = selectedIndex
            selectedIndex = sender.tag

            buttons[previousIndex].isSelected = false
            let previousVC = contentViewControlers[previousIndex]

            previousVC.willMove(toParentViewController: nil)
            previousVC.view.removeFromSuperview()
            previousVC.removeFromParentViewController()

            sender.isSelected = true

            let vc = contentViewControlers[selectedIndex]
            addChildViewController(vc)

            vc.view.frame = contentView.bounds
            contentView.addSubview(vc.view)

            vc.didMove(toParentViewController: self)

            buttons[2].setImage(#imageLiteral(resourceName: "cancel"), for: .normal)
            toogle = false
        } else {
            selectedIndex = sender.tag
            //buttons[selectedIndex].isSelected = false
            if (!toogle) {
                buttons[selectedIndex].setImage(#imageLiteral(resourceName: "add"), for: .normal)
                buttons[selectedIndex].isSelected = true
                let vc = contentViewControlers[selectedIndex]
                addChildViewController(vc)

                vc.view.frame = contentView.bounds
                contentView.addSubview(vc.view)

                vc.didMove(toParentViewController: self)
                toogle = true
            } else {
                buttons[selectedIndex].setImage(#imageLiteral(resourceName: "cancel"), for: .normal)
                let previousVC = contentViewControlers[selectedIndex]

                previousVC.willMove(toParentViewController: nil)
                previousVC.view.removeFromSuperview()
                previousVC.removeFromParentViewController()
                toogle = false
            }
        }


     }
func normalizeView() {

        buttons[2].setImage(#imageLiteral(resourceName: "cancel"), for: .normal)

        toogle = false
    }

ViewController B(点击菜单时)

@IBAction func menuClicked(_ sender: UIButton) {

        if (sender.tag == 0) {
            let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
            let CViewController = storyBoard.instantiateViewController(withIdentifier: "pass_code") as! CViewController

            present(CViewController, animated: true, completion: nil)

        } else if (sender.tag == 1) {
            let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
            let profileViewController = storyBoard.instantiateViewController(withIdentifier: "profile") as! ProfileViewController

            present(profileViewController, animated: true, completion: nil)
        }


        let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let AViewControler = storyBoard.instantiateViewController(withIdentifier: "dashboard") as! AViewController

        //This is the code that tries to change the button from "+" back to default "x" but trows an error that an optional value is nil
        AViewControler.normalizeView()

       //This removes A from the view
        willMove(toParentViewController: nil)
        self.view.removeFromSuperview()
        removeFromParentViewController()

    }

0 个答案:

没有答案