How to get the previous viewcontroller that pushed my current view

后端 未结 10 478
礼貌的吻别
礼貌的吻别 2020-12-28 14:08

The home page of my app has UIButtons, btnIncome and btnExpense. Pressing on this buttons pushes IncomeVC and Expen

10条回答
  •  难免孤独
    2020-12-28 14:29

    If the reason for needing access to the previous view controller is to know what data to get, I would suggest that you instead give the new view controller the data before you push it on the stack. Or at least enough data so that the view controller know how to get the right data, e.g. a enum or constant or something.

    This could be done with a custom initializer, or a property. Take a look at this blog post for an example: "Passing Data Between View Controllers"

    If you are using a storyboard, you can use prepareForSegue:sender to pass the right data. A good tutorial on that can be found here: Beginning Storyboards in iOS 5 Part 2

提交回复
热议问题