简体   繁体   English

在标签栏中的相同选项卡上切换视图而不使用导航控制器

[英]Switch views on the same tab in a tab bar WITHOUT using a navigation controller

I am looking for a way to switch the current view in a tab container to another, all within the same tab and not using a navigation controller. 我正在寻找一种方法将标签容器中的当前视图切换到另一个,所有这些都在同一个选项卡中,而不是使用导航控制器。

I have tried something like this: 我试过这样的事情:

FooViewController *fooViewController = [[FooViewController alloc] initWithNibName:@"FooViewController" bundle:nil];
self.view.window.rootViewController.view.window.rootViewController = fooViewController;
[fooViewController release];

And this: 和这个:

FooViewController *fooViewController = [[FooViewController alloc] initWithNibName:@"FooViewController" bundle:nil];
[self.view removeFromSuperview];
[self.view addSubview:fooViewController.view];
[fooViewController release];

To no avail. 无济于事。

Any ideas? 有任何想法吗?

The method I used was to create a subclass of UIViewController that I used as the root view of 3 child view controllers. 我使用的方法是创建一个UIViewController的子类,我用它作为3个子视图控制器的根视图。 Notable properties of the root controller were: 根控制器的显着属性是:

  • viewControllers - an NSArray of view controllers that I switched between viewControllers - 我在其间切换的视图控制器的NSArray
  • selectedIndex - index of the selected view controller that was set to 0 on viewLoad. selectedIndex - 在viewLoad上设置为0的所选视图控制器的索引。 This is nonatomic, so when the setSelectedIndex was called it did all the logic to put that child view controller in place. 这是非原子的,所以当调用setSelectedIndex时,它会完成将子视图控制器放在适当位置的所有逻辑。
  • selectedViewController - a readonly property so that other classes could determine what was currently being shown selectedViewController - 一个只读属性,以便其他类可以确定当前显示的内容

In the setSelectedIndex method you need to use logic similar to: 在setSelectedIndex方法中,您需要使用类似于以下的逻辑:

[self addChildViewController: selectedViewController];
[[self view] addSubview: [selectedViewController view]];
[[self view] setNeedsDisplay];

This worked really well, but because I wanted to use a single navigation controller for the entire application, I decided to use a different approach. 这非常有效,但因为我想在整个应用程序中使用单个导航控制器,所以我决定采用不同的方法。

I forgot to mention you will want to clear child view controllers every time you add one, so that you don't stack up a ton of them and waste memory. 我忘了提到你每次添加一个时都要清除子视图控制器,这样你就不会堆积大量的视图控制器而浪费内存。 Before the block above call: 在上述块之前:

for (UIViewController *viewController in [self childViewControllers])
    [viewController removeFromParentViewController];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM