一.我们先实现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];
}