如何获取推动我当前视图的前一个视图控制器 [英] How to get the previous viewcontroller that pushed my current view

查看:29
本文介绍了如何获取推动我当前视图的前一个视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用主页有UIButtonsbtnIncomebtnExpense.按下这个按钮分别推送 IncomeVCExpenseVC,这是两个 UIViewControllers 和通过 xib 添加的 UITabBar.tabBar 有 4 个项目.在每个选项卡项上选择添加与 IncomeVCExpenseVC 的子视图相同的四个视图控制器(其中包含 UITableViews),例如 DailyVC,WeeklyVC、MonthlyVC、YearlyVC.(即,对于收入,有每日、每周等和费用相同)(我这样做是因为 IncomeVCExpenseVCUIButton 和所有选项卡通用的 UIView).

The home page of my app has UIButtons, btnIncome and btnExpense. Pressing on this buttons pushes IncomeVC and ExpenseVC respectevely,which are two UIViewControllers with UITabBar added via xib. The tabBar have 4 items. Selecting on each tab item adds same four view controllers(which contains UITableViews) as the subview of IncomeVC and ExpenseVC,like for eg, DailyVC,WeeklyVC,MonthlyVC,YearlyVC.(ie,for Income ,there is daily,weekly etc and same for Expense) (I have done like that because the IncomeVC and ExpenseVC have a UIButton and a UIView which is common for all tabs).

所以问题是,如果单击btnIncome,我必须用与收入相关的数组填充这些表格视图,反之亦然用于费用.如何找到我从哪个 viewController 中选择了不同的选项卡(我需要从我添加为 IncomeVC 和 ExpenseVC 的子视图的 4 个视图中获取它).我是否必须为收入和支出分别制作 8 个不同的视图 4 个?谢谢.

So the problem is that, if click the btnIncome I have to populate those tableviews with the arrays related to Income and vice versa for Expense. How can I find from which viewController I selected the different tabs(I need to get it from the 4 Views I added as the subview of IncomeVC and ExpenseVC). Do I have to make 8 different views 4 each for Income and expense ? Thanx.

推荐答案

如果需要访问以前的视图控制器的原因是为了知道要获取什么数据,我建议您改为将之前的数据交给新的视图控制器你把它压入堆栈.或者至少有足够的数据,以便视图控制器知道如何获取正确的数据,例如枚举或常量或其他东西.

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"

如果您使用的是故事板,您可以使用 prepareForSegue:sender 来传递正确的数据.一个很好的教程可以在这里找到:Beginning Storyboards iniOS 5 第 2 部分

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

这篇关于如何获取推动我当前视图的前一个视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆