防止视图控制器被重置 - UINavcontroller +故事板 +塞格的
我需要一些帮助来为以下“问题”选择“正确”的解决方案。
我使用新的故事板功能将应用程序的所有屏幕链接在一起。基本上结构可深入到:
[Navigation Controller] => [视图控制器 #1] => [标签栏控制器] => [视图控制器 #2]*
**(以及目前不重要的一些其他选项卡)*
我已将第一个视图控制器 (#1) 的转场(推送)附加到选项卡栏控制器后面的视图控制器。当用户在第一个控制器上按下某些东西时,会触发此推送并且工作正常。
// Execute preset segue
[self performSegueWithIdentifier:@"segueEventDetail" sender:self];
当用户(现在位于视图控制器 #2 中)按下导航栏中的后退按钮时,用户会返回。假设他现在再次触发 segue,第二个视图控制器再次显示,但现在已“重置”(空)。 (我相信在阅读了几个论坛和文章后,这是使用 segue 时的标准行为,因为它们每次都会破坏并重新初始化视图控制器?)
这(视图控制器被重置)会带来问题,因为第二个视图控制器的内容是动态的(依赖于来自服务器的 JSON 响应),因此当用户返回时,“需要”视图控制器保持完整(或恢复)。
我找到了几个描述同一问题的来源(见底部),但解决方案各不相同,我需要一些帮助来选择正确的解决方案。
总结:
- 当用户按回键时,如何“保留”/保存视图控制器的状态,同时保留故事板和故事板的使用。最好还有 Segue
自己的想法:
#1 我现在正在考虑将 JSON 响应缓存到我的单例类(并从那里到 PLIST),并在第二个视图控制器中检查该数据是否存在以及而不是重建视图,然后检查是否有任何新数据(恢复正常操作)。
#2 我正在考虑的另一个问题是“绕过”segue 并手动处理视图切换,部分解释在 (Storyboard - 引用 AppDelegate 中的 ViewController) - 这也可能吗?
但也许有一个更简单/更好的选择?
http://www.iphonedevsdk.com/forum/iphone-sdk-development/93913-retaining-data-when-using-storyboards.html" iphonedevsdk.com/forum/iphone-sdk-development/93913-retaining-data-when-using-storyboards.html Storyboard - 引用 AppDelegate 中的 ViewController 如何序列化 UIView?
I need some help in picking the 'right' solution for the following 'problem'.
I use the new storyboard feature to link all the screens of my application together. Basically the structure drills down to:
[Navigation Controller] => [View Controller #1] => [Tabbar Controller] => [View Controller #2]*
**(and some other tabs which are for now not important)*
I have attached a segue (push) from the first View Controller (#1) to the View Controller behind the Tab Bar Controller. This push is triggered when the users presses something on the first controller and works fine.
// Execute preset segue
[self performSegueWithIdentifier:@"segueEventDetail" sender:self];
When the user (which is now in the View Controller #2) presses the back button in the navbar the user goes back. Suppose he now triggers the segue again, the second view controller is shown again but is now 'resetted' (empty). (I believe after reading several fora and articles this is standard behavior when using segue's because these destroy and reinitiliaze the view controller's every time?)
This (the view controller being resetted) poses a problem because the contents of the second view controller is dynamic (depend on a JSON response from the server) and thus it is 'needed' that the view controller remains intact (or is restored) when the user comes back.
I have found several sources (see bottom) describing the same issue, but the solutions vary and I need some help picking the right one.
Summarize:
- How can I 'retain'/save the state of a View Controller when the users presses back, while preserving the use of Storyboard & preferably also Segue's
Own Thoughts:
#1 I'm now thinking of caching the JSON Response to my singleton class (and from there to a PLIST) and checking within the second view controller if this data is present and than rebuild the view after which I check for any new data (resume normal operation).
#2 Another one I'm thinking of is 'bypassing' the segue and manually handle the switch of views , partially explained in (Storyboard - refer to ViewController in AppDelegate) - Is this also possible?
But maybe there is an easier/better option?
http://www.iphonedevsdk.com/forum/iphone-sdk-development/93913-retaining-data-when-using-storyboards.html
Storyboard - refer to ViewController in AppDelegate
How to serialize a UIView?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的!我得到了解决方案。执行以下操作:
在您的 .h 文件中:
在您的 .m 文件中:
所选索引是您要转到的选项卡
Yess!! I got the solution. Do the following:
In you're .h file:
In you're .m file:
The selected index is the tab you want to go
对于将来遇到这个(我的)问题的任何人,这就是我最终“编码”它的方式。
打开故事板并选择“选项卡栏控制器”并打开属性检查器
在“标识符”中填写“标识符”字段
使用第一个视图控制器(请参阅原始帖子中的场景),我创建对视图控制器的全局引用:
firstviewcontroller.h
firstviewcontroller.m
现在要从firstviewcontroller切换,可以使用以下行:
For anyone coming across this (my) question in the future, this is how I ended up 'coding' it.
Open the storyboard and select your 'Tab Bar Controller' and open the Attributes Inspector
Fill in an 'identifier' in the field
With the first view controller (see scenario in original post) I create an global reference to the viewcontroller:
firstviewcontroller.h
firstviewcontroller.m
Now to switch from the firstviewcontroller the following line can be used:
这可能是更简单的解决方案(不使用属性 - 事实上,所有类实例都不需要知道它们的目标控制器,因此只需在推送函数中将其保存为静态):
ps
DLog
只是我的NSLog
变体。但是如何使用 segue 来做到这一点真的很有趣?
This might be even more simple solution (without using properties - in fact, all your class instances don't need to know about their destination controllers, so just save it as static in the pushing function):
p.s.
DLog
is just my variation ofNSLog
.But it's really interesting how to do this with segue?