0

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

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.