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

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

问题描述

我的应用主页有UIButtonsbtnIncomebtnExpense.按下这个按钮会分别按下 IncomeVCExpenseVC,这是两个 UIViewControllers 通过 xib 添加了 UITabBar.tabBar 有 4 个项目.在每个选项卡项目上选择添加相同的四个视图控制器(包含 UITableViews)作为 IncomeVCExpenseVC 的子视图,例如 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天全站免登陆