如何获取推送当前视图的上一个viewcontroller [英] How to get the previous viewcontroller that pushed my current view

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

问题描述

我的应用的主页有 UIButtons btnIncome btnExpense 。按下此按钮可分别推送 IncomeVC ExpenseVC ,这两个 UIViewControllers 通过xib添加 UITabBar 。 tabBar有4个项目。在每个选项卡项上选择添加相同的四个视图控制器(其中包含 UITableViews )作为 IncomeVC 和<$ c的子视图$ c> ExpenseVC ,例如,DailyVC,WeeklyVC,MonthlyVC,YearlyVC。(即,对于收入,有每日,每周等,费用相同)(我这样做是因为 IncomeVC ExpenseVC 有一个 UIButton 和一个 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中找到我选择的不同选项卡(我需要从我添加的4个视图中获取它作为IncomeVC和ExpenseVC的子视图)。我是否必须为收入和费用制作8个不同的观点4?
Thanx。

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 传递正确的数据。有关这方面的好教程可以在这里找到:开始故事板iOS 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

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

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