How to reload rootViewController in navigationCont

2019-09-08 15:19发布

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.

2条回答
Anthone
2楼-- · 2019-09-08 15:50

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)
    }
}
查看更多
来,给爷笑一个
3楼-- · 2019-09-08 16:05

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.

查看更多
登录 后发表回答