重新加载时保留查看状态 [英] Retain view state upon reloading

查看:132
本文介绍了重新加载时保留查看状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一种基本上是一系列用户指令来模拟现实生活系统测试的iPad应用程序,如果组件出现故障(表明需要解决的问题),可以对每个视图进行修改。



我遇到的问题是,视图的默认行为似乎是在我通过层次结构进展的过程中,它保留了每个视图的状态,但如果我进步,然后再次向前移动,将重置屏幕。



我想做的是让每个视图保存其状态,无论用户离开那个屏幕,以便他们可以确信他们的工作是保留,即使他们需要返回上一步。



有什么办法吗?或者我需要从根本上重新考虑我的设计?

解决方案

您需要模型对象以供您查看。这些可能与词典一样简单,或者与每个视图的自定义类相同。



每个视图的控制器必须在视图关闭屏幕之前通过其界面进行的更改来更新其关联的模型。当它重新出现时,VC将使用模型中的信息更新显示。



这跟随着 Model-View-Controller (另见:可可设计模式);您的意见显示信息,您的模型存储信息,控制员在两者之间进行调解和转换。



如何从视图更新模型在很大程度上取决于设计的模型。这是一个可能或可能没有帮助的模型。所有名为 xField 的东西都是 UITextField 的出口。

  //当视图被取消屏幕
- (void)viewWillDisappear {

//假设当创建时,视图控制器被赋予指针
//到相关的模型对象(可能是以前的视图
//控制器)
[model setNameOfHorse:[[self horseNameField] text]];
NSUInteger newBetValue;
newBetValue = [[dollarValueFormatter
numberFromString:[[self betField] text]]
unsignedIntegerValue];
[model setBet:newBetValue];
[model setNote:[[self noteField] text];
}


I am developing an iPad application that is essentially a sequence of user instructions to mimic a real life system test, with the ability to make modifications on each view if components were to fail (indicating issues that will need to be resolved).

The problem I am having is that the default behaviour of the views seems to be that as I progress forward through the hierarchy, it retains the state of each view, but if I progress back and then move forward again it will have reset the screen.

What I would like to do is have each view save its state, regardless of how the user leaves that screen, so that they can be confident that their work is preserved even if they need to return to a previous step.

Is there any way of doing this? Or do I need to fundamentally reconsider my design?

解决方案

You need model objects for your views. These could be as simple as dictionaries or as involved as a custom class for each view.

Each view's controller must update its associated model with the changes made via its interface before the view goes off-screen. When it reappears, the VC will update the display with the information from the model.

This follows the dominant Cocoa paradigm of Model-View-Controller (see also: Cocoa Design Patterns); your views display information, your models store information, and the controllers mediate and translate between the two of them.

How to update a model from the view depends heavily on the design of your model. Here's a mockup that may or may not be helpful. All the things named xField are outlets to UITextFields.

// When the view is taken off screen
- (void) viewWillDisappear {

    // Assume that when created, view controller is given a pointer 
    // to the relevant model object (probably by the previous view
    // controller)
    [model setNameOfHorse:[[self horseNameField] text]];
    NSUInteger newBetValue;
    newBetValue = [[dollarValueFormatter 
                        numberFromString:[[self betField] text]] 
                    unsignedIntegerValue];
    [model setBet:newBetValue];
    [model setNote:[[self noteField] text];
}

这篇关于重新加载时保留查看状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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