0

There are 2 viewControllers in a navigationController: navigationController->root->A enter image description here In viewController A, if a user make some settings and press the left bar button item(Back), I want the root view to renew its layout(some views' size will be changed).

By now, I make it works by adding one more navigationController between the two viewControllers(present modally): navigationController->root->navigationController->A. enter image description here

Is there a way to renew the root viewController with one navigationController?(Screenshot 1)

Thanks.

----- Edited -----

Sample codes:

override func viewWillAppear(_ animated: Bool) {

    creatButtons()

}

func createButtons(){

    let button1 = UIButton()
    ........
    let button2 = UIButton()
    ........
    .......

}

If I create 16 buttons under viewWillAppear(), will all the buttons be duplicated when comes back from A? Their size are all need to be renewed.

4
  • Please you can share you code what you actually want Commented Feb 3, 2017 at 12:22
  • What do you mean by "renew the root viewController with one navigationController"? Commented Feb 3, 2017 at 12:25
  • @NiravD I have uploaded 2 screenshots, it may be clearer. Commented Feb 4, 2017 at 14:47
  • yes every time create new buttons because every time call viewWillAppear when back on screen Commented Feb 6, 2017 at 5:05

2 Answers 2

3

Not sure if I understood your question correctly but here are two alternatives on how to handle this scenario:

If you want to refresh the view hierarchy of root when settings are changed in A just make sure to persist the changes in a place that both root and A can access. Override the -viewWillAppear: (will be triggered on "back" as well) method in root and layout the view according to the settings every time.

Other alternative:

You could create a delegate protocol for A that is implemented by root and assign root as A's delegate when root instantiates or presents A.

A would then invoke its delegate (root) to inform it about the change and let root update its views.

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

2 Comments

It's a good idea to do that in viewWillAppear, but did you mean renewing size or redrawing the whole layout? If redrawing the whole layout, will the layout be duplicated? If only renewing size, it's lots of work to do since it has 16 buttons need to be resize.
I would recommend creating all buttons when creating your view controller in -viewDidLoad and setting their size and appearance from -viewWillAppear:. That way you can always refresh the buttons frame and appearance before showing the view controller each time.
0

RootViewController

class RootViewController: UIViewController ,ProtocolDemo{

    @IBOutlet weak var IBbtnHeight: NSLayoutConstraint!
    @IBOutlet weak var IBbtnWidth: NSLayoutConstraint!

    @IBAction func btnNextTapped(sender: AnyObject) {

        let sec = SecondVC(nibName: "SecondVC", bundle: nil)
        sec.delegate = self
        self.navigationController?.pushViewController(sec, animated: true)
   }

    //Mark:- ProtocolDemo Delegate Method
    func displayMethod(width : Int , height : Int)
   {
       self.IBbtnHeight.constant = CGFloat(height)
       self.IBbtnWidth.constant = CGFloat(width)
       print("Protocol Calling here")
   }
}

SecondVC - You can read as A view Controller

protocol ProtocolDemo
{
    func displayMethod(width : Int , height : Int)//PassYouWantToChange
    //Here you can send full frame if change to update
}

class SecondVC: UIViewController {

var delegate : ProtocolDemo?

    @IBAction func btnBackTapped(sender: AnyObject) {

        delegate?.displayMethod(160, height: 160) //Calling Delegate method
        //pass your data you want to implement
        self.navigationController?.popViewControllerAnimated(true)
    }
}

8 Comments

I have edited my question and added two screenshots, and just wonder if the layout will be created twice?
@jdleung , please see i also update my answer. You need set constraint IBOutlet and back to view controller to update them
Thanks your suggestion! I have no doubt that it can work via protocol. But you know the view has 16 buttons, I have to write more codes. Reloading the whole view is the best way, by now I think the way in the 2nd screenshot is the better choice.
Are you create 16 button programmatically? when you reload your view how to identify your button size is modified?
According to your sample code, when you reload your view(called viewWillAppear) then again add 16 button new in view. I Think delegate is only best option
|

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.