[课堂实践与项目]多视图的Button切换,ToolBarItem切换

本文通过实例介绍了如何在iOS开发中实现在不同视图间切换Button和ToolBarItem的功能。首先创建了YellowViewController和RedViewController两个视图控制器,使用拖拽方式添加Button,并展示了完整视图样式。接着,提供了关键代码展示,特别是在RedViewController中添加ToolBar的实现。文章附带了实际效果截图,帮助理解实现过程。
摘要由CSDN通过智能技术生成

一.我们先实现button切换

我们建立了两个视图

yellowViewCOntroller和RedViewController 两个控制器和视图.

我是用拖拽方式放了两个button.显示当前视图归属.

先看看我们的完整视图样式.

1.2.

然后我贴出关键的代码:

redController:

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    
    [self.view setBackgroundColor:[UIColor redColor]];
    
    UIButton *toYellowButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    [toYellowButton setBackgroundColor:[UIColor yellowColor]];
    [toYellowButton setTitle:@"toYellowButton" forState:UIControlStateNormal];
    
    [toYellowButton addTarget:self action:@selector(changeToYellowView) forControlEvents:UIControlEventTouchUpInside];
    
    
    [self.view addSubview:toYellowButton];
    
    
    [toYellowButton release];
    
    
    

    
	// Do any additional setup after loading the view.
}

- (void)changeToYellowView
{
    LCYellowViewController *yellowViewController = [[LCYellowViewController alloc]initWithNibName:@"YellowView" bundle:nil];
    
    [self presentViewController:yellowViewController animated:YES completion:nil];
    

    [yellowViewController release];
}

2.yellowController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self.view setBackgroundColor:[UIColor yellowColor]];
    
    UIButton *toRedButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    [toRedButton setBackgroundColor:[UIColor redColor]];
    [toRedButton setTitle:@"toRedButton" forState:UIControlStateNormal];
    
    [toRedButton addTarget:self action:@selector(changeToRedView) forControlEvents:UIControlEventTouchUpInside];
    
    
    [self.view addSubview:toRedButton];
    
    
    [toRedButton release];
    

	// Do any additional setup after loading the view.
}

- (void)changeToRedView
{
    
    

    LCRedViewController *redViewController = [[LCRedViewController alloc]initWithNibName:@"RedView" bundle:nil];
    [self presentViewController:redViewController animated:YES completion:nil];
 
    
    
    [redViewController release];
}

二.下面实现 toolBar 的Item点击改变视图

看效果图:  注意看屏幕的下面

1.2.


1.代码如下:

redView代码(只是在viewdidLoad中添加了 toolbar.)这里我贴出全部代码:

.m文件

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    
    [self.view setBackgroundColor:[UIColor redColor]];
    
    UIButton *toYellowButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    [toYellowButton setBackgroundColor:[UIColor yellowColor]];
    [toYellowButton setTitle:@"toYellowButton" forState:UIControlStateNormal];
    
    [toYellowButton addTarget:self action:@selector(changeToYellowView) forControlEvents:UIControlEventTouchUpInside];
    
    
    [self.view addSubview:toYellowButton];
    
    
    [toYellowButton release];
    
    
    
    UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 440, 320, 40)];
    
    toolBar.backgroundColor = [UIColor blackColor];
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithTitle:@"ToYellowView" style:UIBarButtonItemStylePlain target:self action:@selector(changeToYellowView)];
    
    NSArray *arr = [[NSArray alloc]initWithObjects:item, nil];
    [toolBar setItems:arr];
    
    [self.view addSubview:toolBar];
    
    [toolBar release];
    
	// Do any additional setup after loading the view.
}

- (void)changeToYellowView
{
    LCYellowViewController *yellowViewController = [[LCYellowViewController alloc]initWithNibName:@"YellowView" bundle:nil];
    
    [self presentViewController:yellowViewController animated:YES completion:nil];
    

    [yellowViewController release];
}

2.同上,我贴出  yellow代码

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self.view setBackgroundColor:[UIColor yellowColor]];
    
    UIButton *toRedButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    [toRedButton setBackgroundColor:[UIColor redColor]];
    [toRedButton setTitle:@"toRedButton" forState:UIControlStateNormal];
    
    [toRedButton addTarget:self action:@selector(changeToRedView) forControlEvents:UIControlEventTouchUpInside];
    
    
    [self.view addSubview:toRedButton];
    
    
    [toRedButton release];
    
    UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 440, 320, 40)];
    
    toolBar.backgroundColor = [UIColor blackColor];
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithTitle:@"ToRedView" style:UIBarButtonItemStylePlain target:self action:@selector(changeToRedView)];
    
    NSArray *arr = [[NSArray alloc]initWithObjects:item, nil];
    [toolBar setItems:arr];
    
    [self.view addSubview:toolBar];
    
    [toolBar release];

    
    
	// Do any additional setup after loading the view.
}

- (void)changeToRedView
{
    
    

    LCRedViewController *redViewController = [[LCRedViewController alloc]initWithNibName:@"RedView" bundle:nil];
    [self presentViewController:redViewController animated:YES completion:nil];
 
    
    
    [redViewController release];
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值