`
lizhuang
  • 浏览: 893359 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

SegmentControl切换多个UIViewController

 
阅读更多
- (void)viewDidLoad {
    [super viewDidLoad];
    // add viewController so you can switch them later. 
    UIViewController *vc = [self viewControllerForSegmentIndex:self.typeSegmentedControl.selectedSegmentIndex];
    [self addChildViewController:vc];
    vc.view.frame = self.contentView.bounds;
    [self.contentView addSubview:vc.view];
    self.currentViewController = vc;
}
- (IBAction)segmentChanged:(UISegmentedControl *)sender {
    UIViewController *vc = [self viewControllerForSegmentIndex:sender.selectedSegmentIndex];
    [self addChildViewController:vc];
    [self transitionFromViewController:self.currentViewController toViewController:vc duration:0.5 options:UIViewAnimationOptionTransitionFlipFromBottom animations:^{
        [self.currentViewController.view removeFromSuperview];
        vc.view.frame = self.contentView.bounds;
        [self.contentView addSubview:vc.view];
    } completion:^(BOOL finished) {
        [vc didMoveToParentViewController:self];
        [self.currentViewController removeFromParentViewController];
        self.currentViewController = vc;
    }];
    self.navigationItem.title = vc.title;
}

- (UIViewController *)viewControllerForSegmentIndex:(NSInteger)index {
    UIViewController *vc;
    switch (index) {
        case 0:
            vc = [self.storyboard instantiateViewControllerWithIdentifier:@"FooViewController"];
            break;
        case 1:
            vc = [self.storyboard instantiateViewControllerWithIdentifier:@"BarViewController"];
            break;
    }
    return vc;
}
分享到:
评论

相关推荐

    ios-iOS 多个UIViewController之间滑动切换.zip

    当你需要在多个UIViewController之间实现平滑切换时,通常会采用各种技术来提供流畅的用户体验。本教程将深入探讨如何在iOS应用中实现在多个UIViewController之间的滑动切换。 首先,了解UIViewController。...

    UIViewController里添加多个UIViewController

    UIViewController添加多个UIViewController。可以把UIViewController的View当作容器,让其他UIViewController添加进该View,并可以在多个UIViewController间切换。

    swift-UISegmentedControl使用ChildViewController切换UIViewcontroller

    在Swift编程中,`UISegmentedControl`是一个常用的UI组件,它允许用户在多个选项之间进行选择,通常用于切换不同的视图或者展示不同内容。在这个特定的场景中,"swift-UISegmentedControl使用ChildViewController...

    UIViewController+Present.zip

    在iOS开发中,`UIViewController`是苹果提供的一个核心视图控制器类,它是所有自定义视图控制器的基础。在iOS 13及更高版本中,Apple对`UIViewController`的模态弹出行为进行了重大调整,引入了新的交互模式,旨在...

    UIViewController Demo源代码

    Storyboard是一个图形化的用户界面编辑器,可以方便地创建和连接多个视图控制器。对于不使用nib的`UIViewController`,则需在代码中进行实例化和添加到父控制器的视图层次结构中。 5. **源代码分析** `...

    UIViewController

    它允许用户在多个`UIViewController`间平滑滑动。 4. **分割视图控制器(UISplitViewController)**:在iPad应用中,常用于创建主/详情布局,它可以在横屏模式下同时展示两个`UIViewController`实例。 5. **自定义...

    按钮切换两个视图

    总的来说,"按钮切换两个视图"涉及到UI设计、用户交互、视图控制器管理等多个方面。理解并熟练掌握这些知识点,对于开发出用户友好的iOS应用至关重要。通过灵活运用上述技术,开发者可以创造出更丰富的用户体验,...

    swift-一个UIViewController自定义缩放过渡动画

    在RMPZoomTransitionAnimator-master这个项目中,可能包含了更复杂和可定制的实现,包括更多的动画选项、交互支持等。这个库可以帮助开发者轻松地在自己的应用中集成类似的过渡动画效果,提升应用的视觉吸引力和用户...

    实现底部切换标签+左右滑动切换页面

    `UIPageViewController`可以嵌套在`UIViewController`中,使得用户可以通过滑动手势在多个子视图控制器间切换。 对于左右滑动切换页面,我们可以利用`SwipeRefreshLayout`(Android)或`UIPanGestureRecognizer`...

    UIViewController+BackItem分类的实现

    在这个场景下,我们可以创建一个`UIViewController`的分类来实现这个功能。这个分类的名称是"UIViewController+BackItem",它利用了Objective-C的运行时(runtime)机制,以灵活且安全的方式修改导航栏的返回按钮。 ...

    UIViewController+Swizzling 实现页面统计

    1. **创建Category**:创建一个`UIViewController`的Category,例如`UIViewController+PageTracking`,这样可以在不修改原`UIViewController`的基础上添加新的功能。 2. **获取原始IMP**:获取需要替换的方法的原始...

    iOS5 UIViewController 新特性

    1. **Storyboards**:iOS5引入了Storyboard的概念,这是一种全新的界面设计方式,允许开发者在一个单一的文件中组织多个UIViewController及其间的导航关系。通过Storyboard,可以方便地定义Segue(过渡)来实现...

    swift中实现segment在导航栏控制页面之间的转换

    然后,我们需要创建两个或更多的UIViewController子类,分别代表Segment Control切换的各个页面。在storyboard中,将这些页面作为导航控制器的子控制器,并设置适当的segue连接。确保每个页面都具有独特的标识符,这...

    多个xib界面切换

    在iOS应用开发中,"多个xib界面切换"是一个常见的需求,特别是在构建类似网易新闻这样的应用时,用户可以通过左右滑动来浏览不同的内容板块。这种交互设计提供了良好的用户体验,同时也能有效地展示大量的信息。实现...

    页面切换教程 使用了ARC技术

    可能包含一个或多个UIViewController子类,以及相应的视图和逻辑代码。分析项目中的代码可以帮助深入理解页面切换的具体实现和ARC在实际项目中的运用。 总结起来,本教程旨在通过一个实际案例演示如何使用ARC进行...

    深入讲解iOS开发中的UIViewController

    这些控制器都是基于UIViewController的,它们可以管理多个UIViewController实例,从而实现更高级别的界面组织和导航控制。 在实际应用中,我们通常会遇到需要多个UIViewController协同工作的场景。例如,当你点击一...

    UIViewController生命周期详解

    UIViewController生命周期详解及方法调用

    AICustomViewControllerTransition, 为iOS创建定制UIViewController转换的简单而简洁的方法.zip

    AICustomViewControllerTransition, 为iOS创建定制UIViewController转换的简单而简洁的方法 AICustomViewControllerTransition为iOS创建定制UIViewController转换的简单而简洁的方法没有限制,也没有实现...

    iPhone多视图切换

    2. **UINavigationController**:提供了一个在多个UIViewController之间平滑切换的容器。通过导航栏中的“返回”按钮或者编程方式,用户可以前后切换视图控制器。 3. **Storyboard**:是Xcode中用于可视化布局和...

Global site tag (gtag.js) - Google Analytics