FirstApp,iphone开发学习总结6,Navigation的使用
如图所示,实现的功能:
为了展示,我创建一个NavView文件,里面包含3个View:
@interface NavView1 : UIViewController{//代表View1
}
@end
@interface NavView2 : UIViewController {//代表View2
}
@end
@interface NavView3 : UIViewController {//代表View3
}
@end
}
@end
@interface NavView2 : UIViewController {//代表View2
}
@end
@interface NavView3 : UIViewController {//代表View3
}
@end
在NavViewController.m文件中,添加头文件:
#import "NavView.h
- (id)init {
self = [super init];
if (self) {
[self setTitle:@"导航标签"];
}
return self;
}
self = [super init];
if (self) {
[self setTitle:@"导航标签"];
}
return self;
}
在- (void)viewDidLoad中实现:在view添加一个按钮,点击后,push View1。
- (void)viewDidLoad
{
UIButton *navViewBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
navViewBtn.frame = CGRectMake(40.0, 30.0, 240.0, 30.0);
[navViewBtn setTitle:@"To View1" forState:UIControlStateNormal];
[navViewBtn addTarget:self action:@selector(pushView:) forControlEvents:UIControlEventTouchUpInside];
[[self view] addSubview:navViewBtn];
}
{
UIButton *navViewBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
navViewBtn.frame = CGRectMake(40.0, 30.0, 240.0, 30.0);
[navViewBtn setTitle:@"To View1" forState:UIControlStateNormal];
[navViewBtn addTarget:self action:@selector(pushView:) forControlEvents:UIControlEventTouchUpInside];
[[self view] addSubview:navViewBtn];
}
按钮事件实现:
- (void)pushView:(id)sender
{
NavView1 *nv1 = [[NavView1 alloc] init];
[[self navigationController] pushViewController:nv1 animated:YES];
[nv1 release];
}
{
NavView1 *nv1 = [[NavView1 alloc] init];
[[self navigationController] pushViewController:nv1 animated:YES];
[nv1 release];
}
点击按钮后,进入View1,即NavView1,此view拥有一个Button,push至View2:
- (id)init {
self = [super init];
if (self) {
[self setTitle:@"View1"];
}
return self;
}
self = [super init];
if (self) {
[self setTitle:@"View1"];
}
return self;
}
- (void)viewDidLoad
{
UIButton *navViewBtn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
navViewBtn1.frame = CGRectMake(40.0, 30.0, 240.0, 30.0);
[navViewBtn1 setTitle:@"To View2" forState:UIControlStateNormal];
[navViewBtn1 addTarget:self action:@selector(pushView:) forControlEvents:UIControlEventTouchUpInside];
[[self view] addSubview:navViewBtn1];
}
{
UIButton *navViewBtn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
navViewBtn1.frame = CGRectMake(40.0, 30.0, 240.0, 30.0);
[navViewBtn1 setTitle:@"To View2" forState:UIControlStateNormal];
[navViewBtn1 addTarget:self action:@selector(pushView:) forControlEvents:UIControlEventTouchUpInside];
[[self view] addSubview:navViewBtn1];
}
- (void)pushView:(id)sender
{
NavView2 *nv2 = [[NavView2 alloc] init];
[[self navigationController] pushViewController:nv2 animated:YES];
[nv2 release];
}
{
NavView2 *nv2 = [[NavView2 alloc] init];
[[self navigationController] pushViewController:nv2 animated:YES];
[nv2 release];
}
当点击按钮,此时,进入View2。
View2做的事比较多:
1. 进入View3
2. 到达RootView
3. 到达View1
首先,创建3个按钮,并执行事件:
- (id)init {
self = [super init];
if (self) {
[self setTitle:@"View2"];
}
return self;
}
self = [super init];
if (self) {
[self setTitle:@"View2"];
}
return self;
}
- (void)viewDidLoad
{
UIButton *navViewBtn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
navViewBtn2.frame = CGRectMake(40.0, 30.0, 240.0, 30.0);
[navViewBtn2 setTitle:@"Open View3" forState:UIControlStateNormal];
[navViewBtn2 addTarget:self action:@selector(openView:) forControlEvents:UIControlEventTouchUpInside];
UIButton *navViewBtn21 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
navViewBtn21.frame = CGRectMake(40.0, 80.0, 240.0, 30.0);
[navViewBtn21 setTitle:@"To Root" forState:UIControlStateNormal];
[navViewBtn21 addTarget:self action:@selector(popView:) forControlEvents:UIControlEventTouchUpInside];
UIButton *navViewBtn22 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
navViewBtn22.frame = CGRectMake(40.0, 130.0, 240, 30);
[navViewBtn22 setTitle:@"To View1" forState:UIControlStateNormal];
[navViewBtn22 addTarget:self action:@selector(popView1:) forControlEvents:UIControlEventTouchUpInside];
[[self view] addSubview:navViewBtn2];
[[self view] addSubview:navViewBtn21];
[[self view] addSubview:navViewBtn22];
}
{
UIButton *navViewBtn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
navViewBtn2.frame = CGRectMake(40.0, 30.0, 240.0, 30.0);
[navViewBtn2 setTitle:@"Open View3" forState:UIControlStateNormal];
[navViewBtn2 addTarget:self action:@selector(openView:) forControlEvents:UIControlEventTouchUpInside];
UIButton *navViewBtn21 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
navViewBtn21.frame = CGRectMake(40.0, 80.0, 240.0, 30.0);
[navViewBtn21 setTitle:@"To Root" forState:UIControlStateNormal];
[navViewBtn21 addTarget:self action:@selector(popView:) forControlEvents:UIControlEventTouchUpInside];
UIButton *navViewBtn22 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
navViewBtn22.frame = CGRectMake(40.0, 130.0, 240, 30);
[navViewBtn22 setTitle:@"To View1" forState:UIControlStateNormal];
[navViewBtn22 addTarget:self action:@selector(popView1:) forControlEvents:UIControlEventTouchUpInside];
[[self view] addSubview:navViewBtn2];
[[self view] addSubview:navViewBtn21];
[[self view] addSubview:navViewBtn22];
}
点击第一个“Open View3”按钮时:
//这里使用presentModalViewController,由下弹出。
//创建UINavigationController的作用是进入此时的View,navigation为空,必须新建一个。
- (void)openView:(id)sender
{
NavView3 *nv3 = [[NavView3 alloc] init];
UINavigationController *nv = [[UINavigationController alloc] initWithRootViewController:nv3];
[self presentModalViewController:nv animated:YES];
[nv release];
[nv3 release];
}
{
NavView3 *nv3 = [[NavView3 alloc] init];
UINavigationController *nv = [[UINavigationController alloc] initWithRootViewController:nv3];
[self presentModalViewController:nv animated:YES];
[nv release];
[nv3 release];
}
点击第二个“To Root”按钮时://直接进入RootView,此处要检查navigationController是否存在。
- (void)popView:(id)sender
{
[[self navigationController] popToRootViewControllerAnimated:YES];
}
{
[[self navigationController] popToRootViewControllerAnimated:YES];
}
点击第三个“To View1”按钮时,后推一位:
- (void)popView1:(id)sender
{
[[self navigationController] popViewControllerAnimated:YES];
}
{
[[self navigationController] popViewControllerAnimated:YES];
}
//还有一种– popToViewController:animated:,总体类似,参考
差不多都实现了 ,就剩下如何关闭View3了。NavView3的实现:
- (id)init {
self = [super init];
if (self) {
[self setTitle:@"View3"];
}
return self;
}
self = [super init];
if (self) {
[self setTitle:@"View3"];
}
return self;
}
为了美观点,将按钮添加到Navigation的左边并添加一个Label显示此处为View3:
- (void)viewDidLoad
{
UILabel *navViewLbl3 = [[UILabel alloc] initWithFrame:CGRectMake(110.0, 100.0, 100.0, 30.0)];
navViewLbl3.text = @"View3 Here!!";
navViewLbl3.font = [UIFont fontWithName:@"" size:20];
[[self view] addSubview:navViewLbl3];
UIBarButtonItem *navViewBtn3 = [[UIBarButtonItem alloc] initWithTitle:@"Close" style:UIBarButtonItemStyleDone target:self action:@selector(closeView:)];
[self.navigationItem setLeftBarButtonItem:navViewBtn3];
}
{
UILabel *navViewLbl3 = [[UILabel alloc] initWithFrame:CGRectMake(110.0, 100.0, 100.0, 30.0)];
navViewLbl3.text = @"View3 Here!!";
navViewLbl3.font = [UIFont fontWithName:@"" size:20];
[[self view] addSubview:navViewLbl3];
UIBarButtonItem *navViewBtn3 = [[UIBarButtonItem alloc] initWithTitle:@"Close" style:UIBarButtonItemStyleDone target:self action:@selector(closeView:)];
[self.navigationItem setLeftBarButtonItem:navViewBtn3];
}
事件实现:
- (void)closeView:(id)sender
{
[self dismissModalViewControllerAnimated:YES];
}
{
[self dismissModalViewControllerAnimated:YES];
}
持续完善,求指点。
To 6 code download:iOS_FirstApp_6.zip