【问题标题】:Prevent viewcontroller being reset - UINavcontroller + Storyboard + Segue's防止视图控制器被重置 - UINavcontroller + Storyboard + Segue's
【发布时间】:2012-01-09 06:20:43
【问题描述】:

在为以下“问题”选择“正确”解决方案时,我需要一些帮助。

我使用新的故事板功能将我的应用程序的所有屏幕链接在一起。基本上结构可以深入到:

[导航控制器] => [视图控制器#1] => [标签栏控制器] => [视图控制器#2]*

**(以及其他一些暂时不重要的选项卡)*

我已将第一个视图控制器 (#1) 的 segue(推送)附加到选项卡栏控制器后面的视图控制器。当用户在第一个控制器上按下某些东西并且工作正常时,就会触发此推送。

// Execute preset segue
[self performSegueWithIdentifier:@"segueEventDetail" sender:self];

当用户(现在位于 View Controller #2 中)按下导航栏中的后退按钮时,用户会返回。假设他现在再次触发 segue,第二个视图控制器再次显示,但现在“重置”(空)。 (我相信在阅读了几篇论坛和文章后,这是使用 segue 时的标准行为,因为它们每次都会破坏并重新初始化视图控制器?)

这(被重置的视图控制器)造成了一个问题,因为第二个视图控制器的内容是动态的(取决于来自服务器的 JSON 响应),因此“需要”视图控制器保持完整(或恢复)当用户回来时。

我找到了几个描述同一问题的来源(见底部),但解决方案各不相同,我需要一些帮助来选择正确的。

总结:

  • 如何在用户按下后“保留”/保存 View Controller 的状态,同时保留 Storyboard 的使用,最好还保留 Segue 的使用

自己的想法:

#1 我现在正在考虑将 JSON 响应缓存到我的单例类(并从那里到 PLIST),并在第二个视图控制器中检查此数据是否存在,然后重建查看之后我检查是否有任何新数据(恢复正常操作)。

#2 我正在考虑的另一个是“绕过”segue 并手动处理视图切换,部分解释在 (Storyboard - refer to ViewController in AppDelegate) - 这也可能吗?

但也许有更简单/更好的选择?

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?

【问题讨论】:

  • 来自UITabBarController Class Reference "部署标签栏界面时,您必须将此视图安装为窗口的根。与其他视图控制器不同,标签栏界面不应安装为另一个视图控制器。”无关,但很重要。
  • 我不知道!这与我的问题有关,因为我在 tabcontroller 前面确实有另一个视图控制器。

标签: iphone objective-c uiviewcontroller storyboard segue


【解决方案1】:

对于将来遇到这个(我的)问题的任何人,这就是我最终“编码”它的方式。

  • 打开故事板并选择您的“标签栏控制器”并打开属性检查器

  • 在字段中填写“标识符”

  • 使用第一个视图控制器(参见原始帖子中的场景),我创建了对视图控制器的全局引用:

firstviewcontroller.h

@interface YourViewController : UIViewController {

    UITabBarController *tabController;

}

firstviewcontroller.m

//Fill the reference to the tabcontroller using the identifier
tabController = [self.storyboard instantiateViewControllerWithIdentifier:@"tabbar"];

现在要从 firstviewcontroller 切换,可以使用以下行:

[[self navigationController] pushViewController:tabController animated:YES];

【讨论】:

  • 这根本不使用Segues - 你正在退回到程序化的做事风格。我很想看看你如何还能设法使用 segues。
  • 哇……您几乎引用了@Robert-Jan 的回答,但不能给他功劳。您唯一做错的事情而不是仅仅使用他的答案是设置 iVar 而不是属性设置器/获取器。
  • Wilhelmsen,请在提出此类声明之前检查帖子的日期。我在将近 4 个月前发布了上面的代码。 (2011 年 2 月 12 日与 2012 年 3 月 10 日)。
【解决方案2】:

是的!我得到了解决方案。执行以下操作:

在你的 .h 文件中:

@property (strong, nonatomic) UITabBarController *tabController;

在你的 .m 文件中:

@synthesize tabController;

tabController = [self.storyboard instantiateViewControllerWithIdentifier:@"tabbar"];

选中的索引就是你想去的标签

tabController.selectedIndex = 1;

[[self navigationController] pushViewController:tabController animated:YES];

【讨论】:

    【解决方案3】:

    这可能是更简单的解决方案(不使用属性 - 事实上,您的所有类实例都不需要知道它们的目标控制器,因此只需将其保存为静态在推送函数中):

    static UIVewController *destController = nil;
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    if (!storyboard) {
        DLog(Storyboard not found);
        return;
    }
    if (destController == nil) { //first initialisation of destController
        destController = [storyboard instantiateViewControllerWithIdentifier:@"{Your Destination controller identifyer}"];
        if(!destController) {
            DLog(destController not found)
            return;
        }
    }
    //set any additional destController's properties;
    [self.navigationController pushViewController:destController animated:YES];
    

    附言DLog 只是我对NSLog 的变体。

    但是如何用 segue 做到这一点真的很有趣?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-12
      • 1970-01-01
      • 1970-01-01
      • 2020-01-23
      • 2022-01-27
      • 1970-01-01
      • 2012-01-21
      • 2016-03-25
      相关资源
      最近更新 更多