使用分段控件在 UITabBar 选项卡内切换视图
我正在开发一个应用程序,允许将两个媒体库加载到同一个选项卡中。它们具有单独的 XIB 文件并按原样加载。但是,一旦我使用分段控件切换视图,UITabBar
就会消失。
这就是我切换视图的方式:
//Switch Views
- (IBAction)segmentSwitch:(id)sender
{
if([sender selectedSegmentIndex] == 0)
{
MediaViewController *vc = [[MediaViewController alloc] initWithNibName: @"MediaView" bundle:nil];
vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController: vc animated: YES];
}
else if([sender selectedSegmentIndex] == 1)
{
MediaViewControllerTwo*v2 = [[MediaViewControllerTwo alloc] initWithNibName: @"MediaViewTwo" bundle:nil];
v2.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController: v2 animated: YES];
}
}
I am working on an app that allows two media galleries to be loaded into the same tab. They have separate XIB files and are loaded as such. However, once I switch views using the segmented control the UITabBar
disappears.
This is how I am switching the views:
//Switch Views
- (IBAction)segmentSwitch:(id)sender
{
if([sender selectedSegmentIndex] == 0)
{
MediaViewController *vc = [[MediaViewController alloc] initWithNibName: @"MediaView" bundle:nil];
vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController: vc animated: YES];
}
else if([sender selectedSegmentIndex] == 1)
{
MediaViewControllerTwo*v2 = [[MediaViewControllerTwo alloc] initWithNibName: @"MediaViewTwo" bundle:nil];
v2.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController: v2 animated: YES];
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的视图消失的原因是您在其顶部显示了一个模态视图控制器:
如您所知, 模态视图控制器占用整个显示空间。
您想通过分段控制实现什么目标?
编辑:为了更改 UITabBar 中显示的当前视图,您有多种选择。一种是简单地向当前视图添加一个子视图;在您的视图控制器中执行以下操作:
需要调用
viewWillAppear
和viewDidAppear
,在这种情况下框架不会为您调用它们。我假设您的分段控件用于控制视图的一个子区域(即,您的视图的上部部分是分段控件所在的位置,下部部分是一些其他内容的位置)。在这种情况下,我只需尝试替换位于分段控件下方的子视图。技术是一样的。
阅读您的评论后,您可以将一张桌子替换为另一张桌子。或者更好的是,将它们都添加为子视图(全帧),然后根据分段控件选择隐藏其中一个。
The reason why your view disappear is that you are showing a modal view controller on top of it:
As you know a modal view controller takes the whole display space for it.
What are you trying to accomplish with your segmented control?
EDIT: in order to change the current view displayed in the UITabBar, you have several options. One is simply adding a subview to the current view; in your view controller do:
the calls to
viewWillAppear
andviewDidAppear
are required, the framework will not call them for you in this case.I assume that you segmented control is used to control a subarea of your view (i.e., your view has an upper part where the segmented control is and a lower part with some other content). In this case, I would simply try and replace the subview which lays below the segmented control. The technique is just the same.
After reading your comment, you could replace one table with the other one. Or better, have them both added as subview (full frame), and just hide one or the other according to the segmented control selection.
使用
UITabBarController
时,不应以模态方式呈现它们。它们不是正确的工具。您应该切换附加到与选项卡关联的视图控制器的视图。您将如何做到这一点取决于您如何对标签栏进行建模。You should not present them modally when using a
UITabBarController
. They are not the right tools. You should either switch the views attached to the view controller that is associated with the tab. How you would do it depends on how you got your tab bar modelled.