ios - UISplitView - 如果已有实例可见,如何防止加载详细 View 的新实例?

标签 ios objective-c ipad cocoa-touch uisplitviewcontroller

我目前有一个 UISplitViewController。我的主视图 Controller 中有 5 行,点击每一行时都会将其各自的详细 View 放置在屏幕上。它基本上就像某种菜单一样工作。

我的编码方式是在 didSelectRow: 方法中。

这是示例代码

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
        if([indexPath row] == 0)
        {
            firstviewcontroller = [self.storyboard instantiateViewControllerWithIdentifier:@"firstviewcontroller"];

            UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:firstviewcontroller];

            NSArray *vcs = [NSArray arrayWithObjects:[self navigationController], nvc, nil];

            [[self splitViewController] setViewControllers:vcs];

            [[self splitViewController] setDelegate:firstviewcontroller];

我的其他 4 行基本上是相同的。

现在,例如,如果点击第一行,它将显示第一个 View Controller 。当再次点击第一行时,它将用新实例替换当前实例。

如何防止这种情况发生?当我在第一个 View Controller 中填写详细信息(例如文本字段等)然后我不小心点击了第一行,它替换了当前实例并且所有文本字段都是空的并且需要再次填写时,这真的很烦人。

我还想问如何保留viewcontroller实例。例如,我已经在第一个 View Controller 中填写了所需的数据,然后我点击第二行以显示第二个 View Controller 。当我再次点击第一行时,我想要拥有已经完成填充数据的第一个 View Controller 的实例。(有点像“设置”应用程序的工作方式)

最佳答案

您可能应该做的是将菜单 Controller 中的内容 View Controller 的强引用保留为属性。

@private (nonatomic, strong) UINavigationController *navController1;
@private (nonatomic, strong) UINavigationController *navController2;
....

然后

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if([indexPath row] == 0)
    {
        if (!self.navController1) {
            UIViewController *firstviewcontroller = [self.storyboard instantiateViewControllerWithIdentifier:@"firstviewcontroller"];
            self.navController1 = [[UINavigationController alloc] initWithRootViewController:firstviewcontroller];
        }
        if ([self splitViewController] viewControllers] lastObject] != self.navController1) {
            NSArray *vcs = [NSArray arrayWithObjects:[self navigationController], self.navController1, nil];
            [[self splitViewController] setViewControllers:vcs];
            [[self splitViewController] setDelegate:self.navController1.rootViewController];
        }
    }
}

这样它们就不会每次都重新创建,并且您的状态会被保留。

关于ios - UISplitView - 如果已有实例可见,如何防止加载详细 View 的新实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21617738/

相关文章:

iphone - 更新 Cocos2d iPhone 中 HUD 上的标签?

iphone - 仅当在 iphone image-Picker 中完成捕获时单击捕获按钮时如何执行操作

iphone - 动画和调整 UIImageView

iphone - iOS 初学者 : NSMutableArray + int variable for storing array index position (Where is the connection)

iPhone/iPod 和 iPad - 通用应用程序的限制

ios - 如何在 iOS 的 sqlite 数据库中存储音频文件

ios - 在 Swift 中使用 Bool 的 View Controller 扩展

ios - 如何查明随机崩溃的根源

ios - 在 UIImage 周围添加边框

ios - "Camera Roll"和 "My Photo Stream"的 PHAssetCollection 为空