iOS-分段控件控制页面切换

本文介绍了如何在iOS应用中使用分段控件作为导航栏标题视图,通过监听分段控件的选中事件来切换页面。在AppDelegate中设置入口,并确保ViewController继承自导航栏控制器。同时,注意到分段控件不能直接作为导航栏标题视图。通过实例代码展示了如何创建FirstViewController和SecondViewController并实现页面切换效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原理:
把分段控件定义在导航栏标题视图上,在通过分段控件的响应事件的选中索引加载各个控制器视图在self.view上

注意:要在AppDelegate中定义包含导航栏的入口,让ViewController继承导航栏控制器,分段控件不能作为导航栏的标题视图的

实现代码:
新建FirstViewController和SecondViewController两个控制器

AppDelegate.m文件的方法中加入口
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //创建窗口
    _window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    //背景颜色
    _window.backgroundColor = [UIColor whiteColor];
    //可视化
    [_window makeKeyAndVisible];
    ViewController *VC = [[ViewController alloc] init];

    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:VC];

    _window.rootViewController = nav;
    return YES;
}
#import "ViewController.h"
#import "FirstViewController.h"
#import "SecondViewController.h"

@interface ViewController (){
    FirstViewController *firstVC;
    SecondViewController *secondVC;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor lightGrayColor];


    UISegmentedControl *segmt = [[UISegmentedControl alloc] initWithItems:@[@"首页",@"第一个",@"第二个"]];
    segmt.frame = CGRectMake(0, 0, 200, 50);
    segmt.tintColor = [UIColor blueColor];
    segmt.selectedSegmentIndex = 0;
    [segmt addTarget:self action:@selector(segmtAction:) forControlEvents:UIControlEventValueChanged];

    self.navigationItem.titleView = segmt;

    firstVC = [[FirstViewController alloc] init];
    secondVC = [[SecondViewController alloc] init];
    firstVC.view.backgroundColor = [UIColor redColor];
    secondVC.view.backgroundColor = [UIColor greenColor];
}

- (void) segmtAction:(UISegmentedControl *)segmt {

    int index = (int)segmt.selectedSegmentIndex;
    switch (index) {
        case 0:
            [firstVC.view removeFromSuperview];
            [secondVC.view removeFromSuperview];
            break;
        case 1:
            [self.view addSubview:firstVC.view];
            [secondVC.view removeFromSuperview];
            break;
        case 2:
            [self.view addSubview:secondVC.view];
            [firstVC.view removeFromSuperview];
            break;
        default:
            break;
    }
}

@end

运行效果:
这里写图片描述

☆☆☆ “MJCSegmentInterface分段控制器” ☆☆☆ 一款简单的类似百思不得姐主页导航栏下方的那个分段界面控件,类似王者荣耀助手的聊天模块的导航栏下方的分段界面控件等等,简单的说,这是一款分段界面选择器 ☆☆ 如何导入使用框架方法 ☆☆ 可以打开 https://github.com/MJCIOS/MJCSegmentInterface 将项目下载下来,将MJCSlideInterface文件夹放入自己的项目中 项目已支持pod导入,pod 'MJCSegmentInterface',如果没法导入,可以先[pod repo update]更新整个.cocoapods下的所有库... 有啥问题联系我QQ292251588,希望大神们和我多多交流,和大神们一起学习..... 技术交流群612845323,希望和大神们一起学习多多交流,互相学习些技术..... 希望大家多多支持,如果觉得好用,多多帮忙推荐,谢谢大家,谢谢大神,有啥问题,可以提给我... ☆☆ MJCSegmentInterface分段控制器创建(几行代码设置即可使用) ☆☆ NSArray *titlesArr = @[@"啦啦",@"么么",@"啪啪",@"啪啪",@"啪啪",@"啪啪",@"啪啪"]; MJCSegmentFaceControl *segmentsface = [[MJCSegmentFaceControl alloc]init]; segmentsface.frame = CGRectMake(0,64, MJCScreenWidth,MJCScreenHeight); [lala intoTitlesArray:titlesArr hostController:self]; [self.view addSubview:segmentsface]; MJCTestViewController *vc1 = [[MJCTestViewController alloc]init]; vc1.titlesCount = 1; MJCTestTableViewController *vc2 = [[MJCTestTableViewController alloc]init]; vc2.titlesCount = 2; MJCTestViewController1 *vc3 = [[MJCTestViewController1 alloc]init]; vc3.titlesCount = 3; MJCTestViewController *vc4 = [[MJCTestViewController alloc]init]; vc4.titlesCount = 4; MJCTestViewController *vc5 = [[MJCTestViewController alloc]init]; vc5.titlesCount = 5; NSArray *vcarrr = @[vc1,vc2,vc3,vc4,vc5]; [lala intoChildControllerArray:vcarrr]; 可自行修改各个属性,具体属性接口可进入项目查看.. /** 添加控制器的方法(添加控制器按照控制器添加的先后顺序与按钮对应的 */ -(void)intoChildControllerArray:(NSArray *)childControllerArray; /** 添加标题栏的方法 */ -(void)intoTitlesArray:(NSArray *)titlesArray hostController:(UIViewController *)hostController;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值