- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
问题:在我的应用程序中,我在 Storyboard 中使用模态转场展示了一个 UIViewController。该 viewController 有一个 UITableView,用户可以选择一个单元格。然后我关闭那个 viewController 并返回。我想要做的是当 Controller 再次出现时,选定的单元格会突出显示。所以我想将所选单元格的 indexPath 保存在一个属性中,并在 viewWillAppear 中设置该单元格突出显示。但是当 viewController 出现时,它的属性为 nil。所以,我的问题是:
模态视图 Controller 如何记住它的属性?
最佳答案
基本问题是您混合了模型和 View Controller 的职责。
记住应用程序状态是模型的责任。 View Controller 负责将数据绑定(bind)到 View 并验证用户输入。
问题是每个人都作弊。当 View Controller 中有临时状态更改时,通常不需要将其分解为模型,因为 View Controller 和状态数据具有相同的生命周期。
在您的情况下,状态数据的生命周期比 View Controller 更长。将该数据分解为一个模型,然后将该模型应用到 View Controller (单例、从持久存储中读取、在 segue 中应用、……:有很多方法可以做到这一点)。
根据您提供的少量信息,我可以想到如何实现这一目标。
有一个包含数组allItems
、currentItems
、countries
的模型。有一个 currentCountry
属性。覆盖 -setCurrentCountry:
以重建 currentItems
。
- (void)setCurrentCountry:(MyCountryModel *)currentCountry
{
_currentCountry = currentCountry;
self.currentItems = [self filterAllItemsByCountry:currentCountry];
}
项目 View Controller 将包含模型的一个实例。当国家 View Controller 被推送时,模型可以在 segue 中传递。 countries View Controller 将使用 countries
作为它的数据源。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
cell = …
MyCountryModel *country = self.model.countries[indexPath.row];
cell.selected = country == self.model.currentCountry;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
MyCountryModel *country = self.model.countries[indexPath.row];
self.model.currentCountry = country;
}
关于iOS:模态 viewController 如何记住它的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25656050/
我现在在另一个 ViewController 之上展示了一个几乎透明的 ViewController,但它禁用了与下面的 ViewController 的交互。这是我目前使用的代码: topContr
我有一个 View Controller ,其工具栏上有 3 个 UIButtons,可以打开一个新的 View Controller 作为弹出窗口。我在 Storyboard 中创建了 Segues
这个问题在这里已经有了答案: Delegates in swift? (15 个答案) 关闭 3 年前。 在我的场景中,我试图在关闭 View Controller 期间将值从 ViewContro
假设我有两个 viewController,AViewController 和 BViewController。 AViewController 嵌入在 NavigationController 中(
我有 3 个 View Controller A、B 和 C。 我在 vcA 上,我使用 推送 vcB [self.navigationController pushViewController:vc
我想知道如何在 VC3 和 VC1 之间传递数据。 我拥有的是 Root View (VC1)。然后你输入一个tableView,如果你选择一个项目,你将进入tableView2(VC3) - 类别
我在使用 Unwind Segue 时遇到问题。我有 3 个 ViewController,它们都在名为 ViewController.swift 的类下。我试图获取最后一个 ViewControll
我有一个应用程序,其左上角有一个汉堡菜单。按下时,菜单会以 90% 的不透明度淡入当前 ViewController,显示应用程序的主要导航选项。 我使用下面的代码从页面呈现 viewControll
我的问题是,当我呈现一个 UIViewController 时,呈现的 View 变黑了。 我有一个名为 mainViewController 的 UIViewController,它是我窗口的 Ro
例如,当我呈现UIActivityViewController或UIAlertController时,我仍然可以看到背景viewController,我不想在viewContollerA中嵌入view
我从second viewController进入了third viewController,但是我不想回到第二个viewController,我需要直接回到我的第一个viewController,我
我的应用程序是标签栏(+ 导航)应用程序。在 FirstViewController 中,我调用 onModalView。 -(void) onFilter { FilterViewControl
Photo Sharing 部分是我写的一个ViewController,当我按下正确的项目时,PhotoSharingViewController 会动画出现。 这是我的代码: PhotoShari
我正在尝试在另一个 View Controller 中使用 subview Controller 的应用程序。我有一个 VC,我正在实例化另一个 VC,并在外部 VC 内使用它自己的 xib。 我使用
我正在寻找有关包含 ViewController 的滚动的想法。我需要知道包含 View Controller 的位置。我已经尝试过 UIPageViewController 但我无法让滚动委托(de
我有一个带有嵌入式 NavigationController 的基本 ViewController 来提供工具栏。 ToolBar 有三个按钮,用于通过 segue 调用 ViewController
我的应用程序中的伙计们,我在应用程序委托(delegate)方法中有一些代码 application:didFinishLaunchingWithOptions:确定初始 View Controlle
我遇到了一个问题,我将对此进行描述,并且发现了一些类似的问题,但我认为与我的问题无关。 我有一个 UITabBarController,其中有 2 个用于 VC1 和 VC2 的选项卡。 VC1 转到
我的应用程序中有一个计时器。如果计时器达到 0,则用户将被带到失败页面。当这种情况发生时,之前的 ViewController 没有被关闭,导致更多的内存被占用。 如果出现失败页面,我希望关闭之前的
在问这个问题之前,我当然做了一些调查。我在 google 和 stackoverflow 上找到的链接或主题之一是: How do I load another view controller fro
我是一名优秀的程序员,十分优秀!