Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone UIViewController不经常重新实例化时,UINavigationBar中的UISegmentedControl将消失_Iphone_Ios_Uiviewcontroller_Uinavigationcontroller_Uinavigationbar - Fatal编程技术网

Iphone UIViewController不经常重新实例化时,UINavigationBar中的UISegmentedControl将消失

Iphone UIViewController不经常重新实例化时,UINavigationBar中的UISegmentedControl将消失,iphone,ios,uiviewcontroller,uinavigationcontroller,uinavigationbar,Iphone,Ios,Uiviewcontroller,Uinavigationcontroller,Uinavigationbar,我使用导航栏中的UISegmentedControl在应用程序中的UIViewController之间切换。但是,在我多次切换UIViewControls后,UISegmentedControl将从导航栏中消失。只有在每次按下分段控件时,视图控件未初始化并重新分配时,才会发生这种情况。换句话说,如果每次我想在应用程序中切换视图时分配一个新的UIViewController,就会观察到预期的行为(例如,UISegmentedControl不会消失)。但是,这不是一个实用的解决方案,因为我想在用户切

我使用导航栏中的UISegmentedControl在应用程序中的UIViewController之间切换。但是,在我多次切换UIViewControls后,UISegmentedControl将从导航栏中消失。只有在每次按下分段控件时,视图控件未初始化并重新分配时,才会发生这种情况。换句话说,如果每次我想在应用程序中切换视图时分配一个新的UIViewController,就会观察到预期的行为(例如,UISegmentedControl不会消失)。但是,这不是一个实用的解决方案,因为我想在用户切换视图时保存隐藏视图的状态

我有一个简单项目的回购协议,在该协议中观察到了这一点。该项目由两个提交组成:一个是观察到此问题的较旧提交,另一个是通过不断重新实例化“解决”此问题的较新提交


有关本回购协议中较早提交的更多详细信息:

accountingAppDelegate中的
didFinishLaunchingWithOptions
是应用程序的两个视图控制器(RootViewController和CalendarViewController)在应用程序的生命周期内都被实例化的位置

UISegmentedControl是从名为SegmentedControl.xib的NIB文件加载的,该文件位于
didFinishLaunchingWithOptions
方法中

accountingAppDelegate中的
toolBarItems
方法对于viewcontroller的
ViewDidDisplay
方法都是一种方便的方法,其中设置了每个viewcontroller的
toolBarItems
属性。根据我的理解(查找“显示工具栏”),UINavigationController查看此属性以为当前视图分配导航工具栏中的项目

新提交的唯一区别在于,每次用户从一个实例切换到另一个实例时,都会创建新的RootViewController和CalendarViewController实例



我的问题是:为什么常量重新实例化可以解决这个问题?更重要的是,我能否防止UISegmentedControl消失并保留应用程序的视图状态?

根据评论,我认为这不是一种合适的方法。 基本上,当您在
UINavigationController
中交换
rootViewController
时,您是在重置它!因此,您需要再次初始化ViewController

我的建议是你做以下几点:

  • 创建
    RootViewController
    的实例(例如
    rvc
  • 创建
    CalendarViewController
    的实例(例如
    cvc
  • 将它们添加到
    NSArray
    NSMutableArray
  • 创建
    UINavigationController
    initWithRootViewController:
    UIViewController的实例(不是
    rvc
    cvc
  • 从你想先展示的那个开始:例如

    pushViewController:rvc动画:否

  • 当您需要交换时:

    PoptorootViewController激活:否

    pushViewController:cvc动画:否

  • 从UINavigationController的角度来看,您正在从用户的角度弹出和按下ViewController——因为它没有动画,所以您正在交换

    不过,如果你想制作动画。你不能这样做。 首先,您不应该使用
    UIViewController
    ,而应该使用
    UIViewController
    ,使用UISegment添加UIToolBar,并将
    cvc
    rvc
    的视图添加到
    UIViewController
    ,必要时,使用动画交换它们

    以及其他一些建议:
    此方法<代码>[NSArray arrayWithObject:[[CalendarViewController alloc]init]]不合适。当您将对象传递给NSArray时,NSArray拥有该对象,因此它将保留该对象。最好不要初始化/分配它,或者如果必须,在将它传递给
    NSArray
    后,将其释放。

    如果我正确理解了您的代码,您正在做的是:1。在appDelegate中创建UINavigationController的实例。2.您创建了RootViewController的一个实例,我假设它是UIViewController的一个自定义类。3.您创建了CalendarViewController的一个实例,它是UIViewController的一个自定义类。4.您可以将它们交换为UINavigationController的rootViewController(在项目符号1中)。是吗?是的。这是正确的。更具体地说,我使用UINavigationController的“SetViewController:”方法间接更改UINavigationController的rootViewController属性。请参阅accountingappdelegate中的indexDidChangeForSegmentedControl方法,以查看发生这种情况的位置。我根据提出了此方法,因此我知道其他人正在这样做。在可维护性和代码重用方面,本文还特别警告不要使用包含子ViewController数组的管理视图控制器。我更感兴趣的是为什么我的方法不起作用。你是什么意思,我正在“重置”UINavigationController?你介意扩展一下吗?好的,根据你给我的链接,情况是这样的:当你说[self.navigationController setViewControllers:theViewControllers animated:NO];您正在使用一组新的ViewController替换UINavigationController中的ViewController阵列。在此,此阵列仅包含一个viewController。通过这样做,您可以放置所有内容,包括包含UISegmentControl的titleView。下一行显示:incomingViewController.navigationItem.titleView=AseSegmentedControl;哪个是放回,或者最好说将段控制器添加回标题视图。但是,如果仔细查看回购,您会发现我没有使用incomingViewController.navigationItem.titleView来设置我的UISegmentedControl。这是因为我需要导航栏来显示其他东西