Is prepareForSegue right way of passing value between view controllers

后端 未结 4 1854
迷失自我
迷失自我 2021-01-05 16:02

I\'m trying to learn Swift and I\'m trying to develop the famous note application.

There is an array bound to a tableview and another view for adding notes. At secon

4条回答
  •  醉酒成梦
    2021-01-05 16:48

    I would recommend passing data via prepareForSegue in most cases. It's pretty simple to set up and easy to understand.

    However, I would recommend never updating UI elements (labels, text fields, etc.) on the destination view directly. In my opinion, this is bad coupling that creates a lot of problems.

    Instead, create a property or properties on the destination view controller that the caller can set in prepareForSegue to pass data to it. These should be special purpose properties used exclusively for passing data. The destination view controller is then in charge of using the data in these properties to update its UI or internal state.

    Delegation is a valid approach, but I find it to be overkill for most situations. It requires more setup and is more abstract. This abstraction isn't needed in a lot of view controller relationships. If you discover you need to reuse a view controller, you can always refactor to use delegation later.

提交回复
热议问题