Search code examples
swiftuinavigationcontrolleruibuttonhidden

How to HIDE View when tap on button swift?


In first view controller we have two buttons

if we tap on first view controller oneButn i need to hide onebutnContainerView in secondview controller

if we tap on first view controller secndButn i need to hide twobutnContainerView in secondview controller

in first view controller viewController.oneButnContainerView.isHidden = true getting error:

Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value

first view controller code:

 class firstViewController: UIViewController{
 @IBAction func oneButn(_ sender: UIButton) {
    self.view.endEditing(true)

    let viewController = self.storyboard?.instantiateViewController(withIdentifier: "NewZoomAddressViewController") as! NewZoomAddressViewController;
                viewController.delegate = self

            viewController.oneButnContainerView.isHidden = true
            viewController.twobutnContainerView.isHidden = false

                self.navigationController?.pushViewController(viewController, animated: true);

}
@IBAction func secndButn(_ sender: UIButton) {
    self.view.endEditing(true)

    let viewController = self.storyboard?.instantiateViewController(withIdentifier: "NewZoomAddressViewController") as! NewZoomAddressViewController;
                viewController.delegate = self

              viewController.oneButnContainerView.isHidden = false

            viewController.twobutnContainerView.isHidden = true

                self.navigationController?.pushViewController(viewController, animated: true);
 }
}

I have outlets for two views in Second view controller

     @IBOutlet weak var oneButnContainerView: UIView!
     @IBOutlet weak var twoButnContainerView: UIView!

how to hide seconviewcontroller view in firstviewcontroller


Solution

  • That's because you are trying to hide a View that wasn't initialized yet. As a rule of thumb, keep in mind that when you instantiate a viewController, you can only access its data not its views. You have 2 ways of fixing this:

    1. Create 2 variables inside secondViewController:

    var isOneButnContainerViewHidden: Bool = false var isTwoButnContainerViewHidden: Bool = false

    1. Assign a value to those 2 variables inside firstViewController:

         let viewController = self.storyboard?.instantiateViewController(withIdentifier: "NewZoomAddressViewController") as! NewZoomAddressViewController;
          viewController.delegate = self
          viewController.isOneButnContainerViewHidden= false
          viewController.isTwoButnContainerViewHidden= true
          self.navigationController?.pushViewController(viewController, animated: true);
      
    2. Now inside your secondViewController's viewDidLoad or viewWillAppear, hide/show your buttonContainerViews based on the value of the 2 variables created:

        oneButnContainerView.isHidden = isOneButnContainerViewHidden
        twoButnContainerView.isHidden = isTwoButnContainerViewHidden
    

    The second way involves forcing the viewController to layout its views by calling loadViewIfNeeded() on the secondViewController before accessing its views (in this case you are trying to hide/show the views).