在Storyboard中使用viewController类的一个实例 [英] Using one instance of a viewController class in Storyboard

查看:97
本文介绍了在Storyboard中使用viewController类的一个实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用故事板在Xcode 5中编写iOS7应用程序。在应用程序的一部分中,我需要三个共享相同 viewController 类的屏幕。这些屏幕是 UIViewControllers 。我使用 UISegmentControl 根据条件从屏幕到屏幕。如果用户没有完成某些步骤,我禁用了控件。

I am writing an iOS7 app in Xcode 5 with storyboards. In a part of the app, I need three screens that shared the same viewController class. These screens are UIViewControllers. I use a UISegmentControl to go from screen to screen based on conditions. I disabled the control if the user had not completed certain steps.

我使用 BOOL 值检查某些步骤是否已完成并将其值设置为是/否。

I used a BOOL value check if certain steps had been completed and set its value to YES / NO.

问题是当我想回到最后一个屏幕时 - 我正在获取viewController的新实例类。这有两个问题:

The problem is when I want to go back to the last screen - I am getting a new instance of my viewController class. This has two problems:


  1. 每次用户在两个视图之间进行内存增长

  2. BOOL当新实例加载时,value和所有其他属性为nil。

在我的分段控件中,这就是我访问视图的方式:

In my segment control this is how I get to the views:

-(void)segmentcontrol:(UISegmentedControl *)segment
{

    if (segment.selectedSegmentIndex == 0)
        {

   self.viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"stepOne"];
        [self presentViewController:self.viewController animated:NO completion:nil];
        }
    else if (segment.selectedSegmentIndex == 1 ){
        self.viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"stepTwo"];
        [self presentViewController:self.viewController animated:NO completion:nil];
    }else {
    }
}

viewController 是我的 BaseViewController 的子类 - 我用于在所有屏幕上保持不变的UI元素。

This viewController is a subclass of my BaseViewController - which I used for UI elements that is constant across all screens.

当我将段控件更改为a时,我想要做的是返回 viewController 类的相同实例另一个视图,使用相同的类。

What I want to do is return the same instance of the viewController class when I change the segment control to a another view, using the same class.

这一切都可能吗?

推荐答案

不清楚你为什么使用 presentViewController:animated:completion:但看起来你的做法是错误的。

Not clear why you're using presentViewController:animated:completion: but it looks like you're doing things in the wrong way.

你想要做的是创建一个容器控制器。因此,托管分段控件的视图控制器会创建许多视图控制器实例,并将它们添加为子视图控制器。现在,当选择了段时,您将获得所选索引处的子项,从其超级视图中删除旧视图控制器视图,并将新视图控制器视图添加为子视图。

What you want to be doing is creating a container controller. So, the view controller which hosts the segmented control creates a number of view controller instances and adds them as child view controllers. Now, when the segments are selected you get the child at the selected index, remove the old view controllers view from its superview and add the new view controllers view as a subview.

你不需要这样做,但它可能是最干净的。你的内存目前增长是因为你使用 instantiateViewControllerWithIdentifier:。您真正需要做的就是保留一组视图控制器并重用而不是重新创建。也就是说,不断呈现视图控制器并不明智。

You don't need to do it like that, but it will probably be cleanest. Your memory grows currently because you use instantiateViewControllerWithIdentifier:. All you really need to do is to keep an array of view controllers and reuse instead of recreating. That said, continually presenting view controllers is not wise.

这篇关于在Storyboard中使用viewController类的一个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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