重新加载时保持视图状态 [英] Retain view state upon reloading

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

问题描述

我正在开发一个 iPad 应用程序,它本质上是一系列用户指令来模拟现实生活中的系统测试,如果组件出现故障(指示需要解决的问题),则能够对每个视图进行修改.

我遇到的问题是,视图的默认行为似乎是当我在层次结构中前进时,它会保留每个视图的状态,但是如果我向后前进然后再次前进,它将重置屏幕.

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

有没有办法做到这一点?还是我需要从根本上重新考虑我的设计?

解决方案

您的视图需要模型对象.这些可以像字典一样简单,也可以像每个视图的自定义类一样涉及.

在视图离开屏幕之前,每个视图的控制器必须使用通过其界面所做的更改来更新其关联模型.当它再次出现时,VC 将使用来自模型的信息更新显示.

这遵循了 的主导 Cocoa 范式模型-视图-控制器(另见:可可设计模式);您的视图显示信息,您的模型存储信息,并且控制器在两者之​​间进行调解和转换.

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

//当视图离开屏幕时- (void) viewWillDisappear {//假设创建时,视图控制器被赋予一个指针//到相关的模型对象(可能是之前的视图//控制器)[模型 setNameOfHorse:[[self horseNameField] text]];NSUInteger newBetValue;newBetValue = [[dollarValueFormatternumberFromString:[[self betField] 文本]]无符号整数值];[模型 setBet:newBetValue];[模型集注:[[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天全站免登陆