nicklockwood / layout

A declarative UI framework for iOS
MIT License
2.23k stars 97 forks source link

layoutdidload not called when nested layout used #154

Closed wpcfan closed 5 years ago

wpcfan commented 5 years ago

I modify the sample app to make BoxViewController implements the LayoutLoading protocol, and in BoxViewController's func layoutDidLoad(_: LayoutNode) , I am trying to set something up, but apparently it did not work as I expected -- the method will not be called.

Is this behavior by design? if it is, what else can I do to set up something after subview's layout node loaded

nicklockwood commented 5 years ago

@wpcfan the LayoutLoading protocol is only used for classes that actually call loadLayout() themselves to load their contents from an XML file.

For a view or controller whose layout is loaded by another class (like BoxesViewController) you can override the layoutNode property and add your own didSet handler. This has actually already been done in BoxesViewController, and is used to set the default state:

 @IBOutlet var layoutNode: LayoutNode? {
    didSet {
        layoutNode?.setState(["isToggled": toggled])
    }
}

You can add any custom setup you want inside didSet, and use the same pattern in your own controllers/

wpcfan commented 5 years ago

@nicklockwood Thanks for pointing it out. But I think a lifecycle hook is better, sometimes, I need to setup something when all things ready including the outlets connected. But in the setter, we cannot guarantee all things ready. I want to have a hook like viewDidLoad

for example, if we want to do something related to tableview in layoutNode setter, the tableview will be nil, of course, I can do tableview stuff in tableView's setter, but the point is I think a lifecycle hook that ensure everything is ready can do better job

@IBOutlet var tableView: UITableView?
@IBOutlet var layoutNode: LayoutNode? {
    didSet {
        layoutNode?.setState(["isToggled": toggled])
        tableView?.dosomething()
    }
}
wpcfan commented 5 years ago

finally figure it out , so for nested xml, I am able to do the setups in parent controller's layoutDidLoad. closing the issue.