如何访问堆栈中的 NSDictionary 2 层?
(iOS) 我有一个 UITableView 作为根视图(屏幕 1)。当选择一个单元格时,它会推送一个新的 UIView(屏幕 2),其中包含两个按钮。当按下这些按钮之一时,就会推送一个新的 UIView(屏幕 3)。 我想将一些键值对从屏幕 1 传递到屏幕 2 和屏幕 3。使用以下代码,我可以毫无问题地将值传递到屏幕 2,但屏幕 3 返回零作为值,无论哪个单元格是摘了。任何关于为什么会发生这种情况的帮助都会很棒。 (另一种方法是在屏幕 2 中使用 switch-cases 将值传递到屏幕 3)
屏幕 1:
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
NSDictionary *sentItems = [NSDictionary dictionaryWithObjectsAndKeys:indexPath, @"indexPath", nil];
[destination setValue:sentItems forKey:@"sentItems"];
(iOS)
I have a UITableView as a root view (screen 1). when a cell is selected, it pushes a new UIView (screen 2) which contains two buttons. When one of those buttons is pressed, a new UIView is pushed (screen 3).
I would like to pass some key-value pairs from screen 1 to both screen 2 and screen 3. Using the following code I can pass values to screen 2 with no problem, but screen 3 returns zero as the value, regardless of which cell is picked. Any help on why this is happening would be great. (The alternative is to use switch-cases in screen 2 to pass values to screen 3)
Screen 1:
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
NSDictionary *sentItems = [NSDictionary dictionaryWithObjectsAndKeys:indexPath, @"indexPath", nil];
[destination setValue:sentItems forKey:@"sentItems"];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为最简单的解决方案是传递一个
sentItems
或类似的字典,其中包含在初始化第二个和第三个视图期间可能需要的所有值。因此,如果您需要视图 2 的值
X,Y
和视图 3 的值A,B,C
或备用视图的值D,E,F
按钮视图 3,在显示之前将定义有X,Y,A,B,C,D,E,F
的字典传递给视图 2,然后在选择一个视图时将适当的值传递给视图 3按钮但在显示该视图之前。这样就完全不用再查看堆栈了。如果您的第一个视图实际上是应用程序的根视图,您还可以选择在应用程序委托类中定义此字典,以便可以通过
[[UIApplication共享应用程序]
委托]
。The solution I would think would be easiest is to pass along a
sentItems
or similar dictionary with all the values you could need during initialisation of the second and third views.So if you need values
X,Y
for view 2 andA,B,C
for view 3 orD,E,F
for the alternate button view 3, pass a dictionary withX,Y,A,B,C,D,E,F
defined to view 2 before showing it, then pass the appropriate values to view 3 when you select one of the buttons but before showing that view.That saves looking back in the stack at all. If your first view is actually the root view of the application, you also have the option to define this dictionary in your application delegate class, so it becomes globally accessible via
[[UIApplication sharedApplication]
delegate]
.