新浪微博客户端(10)-切换多个fragment

本文介绍了一个iOS应用中实现三个不同视图控制器之间的切换逻辑。通过按钮点击事件,可以隐藏当前显示的视图并展示另一个视图控制器。每个视图控制器都有特定的frame设置。

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

 

ViewController.m

#import "ViewController.h"
#import "DJOneViewController.h"
#import "DJTwoViewController.h"
#import "DJThreeViewController.h"


@interface ViewController ()

@property (nonatomic,strong) DJOneViewController *vc1;
@property (nonatomic,strong) DJTwoViewController *vc2;
@property (nonatomic,strong) DJThreeViewController *vc3;


- (IBAction)btn1Click;

- (IBAction)btn2Click;

- (IBAction)btn3Click;


@end

@implementation ViewController



- (DJOneViewController *)vc1 {

    if (!_vc1) {
        DJOneViewController *vc = [[DJOneViewController alloc] init];
        vc.view.frame = CGRectMake(60, 0, 600, 300);
        self.vc1 = vc;
    }
    return _vc1;
}


- (DJTwoViewController *)vc2 {

    if (!_vc2) {
        DJTwoViewController *vc = [[DJTwoViewController alloc] init];
        vc.view.frame = CGRectMake(60, 0, 600, 320);
        self.vc2 = vc;
    }
    return _vc2;

}

- (DJThreeViewController *)vc3 {

    if (!_vc3) {
        DJThreeViewController *vc = [[DJThreeViewController alloc] init];
        vc.view.frame = CGRectMake(60, 0, 600, 320);
        self.vc3 = vc;
    }
    return _vc3;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)btn1Click {

    [self.vc2.view removeFromSuperview];
    [self.vc3.view removeFromSuperview];
    
    [self.view addSubview:self.vc1.view];
}

- (IBAction)btn2Click {

    [self.vc1.view removeFromSuperview];
    [self.vc3.view removeFromSuperview];
    [self.view addSubview:self.vc2.view];
    
    
}

- (IBAction)btn3Click {
    [self.vc1.view removeFromSuperview];
    [self.vc2.view removeFromSuperview];
    [self.view addSubview:self.vc3.view];
}
@end

 

最终效果:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值