UINavigationController

通过UINavigationController实现页面推转

实现逻辑结构:

1.首先window,UINavigationController就创建在window上,并控制谁是首页,然后让window管理创建的导航控制器.

2.用导航控制器控制跳转

(1)导航栏可以建立item,但不能创建button,可以用button初始化一个item,但不能把button加到导航栏.

(2)也可以直接通过UIBarButtonItem直接初始化出item

(3)页面中无论item还是button实现跳转时都使用导航控制器实现,主要方法有pushViewController(类似入栈即推进到下一页),popViewControllerAnimated(类似出栈,返回前一页),popToViewController(通过控制后面参数直接实现跳转到某一页,参数一半通过数组控制).

(4)通过UINavigationController即可实现页面控制跳转,此时ViewController就不需要了.


主要.m文件代码:
//
//  AppDelegate.m
//  lesson_navgation
//
//  Created by lanouhn on 15-1-8.
//  Copyright (c) 2015年 lanouhn. All rights reserved.
//

#import "AppDelegate.h"
#import "FirstViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

//应用程序已经配置完成执行的方法
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    //1.创建window
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    //2.显示window
    [self.window makeKeyAndVisible];
    self.window.backgroundColor=[UIColor purpleColor];
    
    //3.创建导航控制器
    FirstViewController *first = [[FirstViewController alloc]init];
    //导航控制器就把first当做首页显示
    UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:first];
    
    //4.让window管理创建的导航控制器
    self.window.rootViewController = nav;
    
    
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
//
//  FirstViewController.m
//  lesson_navgation
//
//  Created by lanouhn on 15-1-8.
//  Copyright (c) 2015年 lanouhn. All rights reserved.
//

#import "FirstViewController.h"
#import "SecViewController.h"
@interface FirstViewController ()

@end

@implementation FirstViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor greenColor];
    // Do any additional setup after loading the view.
    //buttonf方法实现页面推转
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.backgroundColor = [UIColor redColor];
    btn.frame=CGRectMake(100,350,120,50);
    [btn setTitle:@"Next" forState:UIControlStateNormal];
    [self.view addSubview:btn];
    [btn addTarget:self action:@selector(dobutton) forControlEvents:UIControlEventTouchDown];
    
    //1.设置标题
    //(1)
    //self.title = @"首页";
    //(2)
    self.navigationItem.title=@"首页";
    //(3)----添加图片等自定义情况时使用
    //系统自定义导航条高度44
    /*UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 40)];
    label.text=@"首页";
    self.navigationItem.titleView=label;
     */
    
    //对导航条设置一个颜色
    [self.navigationController.navigationBar setBackgroundColor:[UIColor redColor]];
    
    //设置导航条上的按钮
    UIBarButtonItem *item=[[UIBarButtonItem alloc]initWithTitle:@"下一页" style:UIBarButtonItemStylePlain target:self action:@selector(doitem)];
    self.navigationItem.rightBarButtonItem=item;
    
    
    //用button初始化一个item,但不能把button加到导航条
    UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 60, 40)];
    [button setTitle:@"保存" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(dosave) forControlEvents:UIControlEventTouchDown];
    //创造一个item
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = leftItem;
}
-(void)dobutton
{    SecViewController *sec = [[SecViewController alloc]init];
    //用导航控制器推下一个界面
    [self.navigationController pushViewController:sec animated:YES];
}
-(void)doitem
{
    SecViewController *sec = [[SecViewController alloc]init];
    //用导航控制器推下一个界面
    [self.navigationController pushViewController:sec animated:YES];
}
-(void)dosave
{
    NSLog(@"Dosave");
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
//
//  SecViewController.m
//  lesson_navgation
//
//  Created by lanouhn on 15-1-8.
//  Copyright (c) 2015年 lanouhn. All rights reserved.
//
#import "SecViewController.h"
#import "ThdViewController.h"
@interface SecViewController ()

@end

@implementation SecViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor greenColor];
    // Do any additional setup after loading the view.
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.backgroundColor = [UIColor redColor];
    btn.frame=CGRectMake(100,350,120,50);
    [btn setTitle:@"Back" forState:UIControlStateNormal];
    [self.view addSubview:btn];
    [btn addTarget:self action:@selector(dobutton) forControlEvents:UIControlEventTouchDown];
    
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn1.backgroundColor = [UIColor redColor];
    btn1.frame=CGRectMake(100,500,120,50);
    [btn1 setTitle:@"next" forState:UIControlStateNormal];
    [self.view addSubview:btn1];
    [btn1 addTarget:self action:@selector(dobutton1) forControlEvents:UIControlEventTouchDown];
}
-(void)dobutton
{
    //SecViewController *sec = [[SecViewController alloc]init];
    //用导航控制器推下一个界面
    [self.navigationController popViewControllerAnimated: YES];
}
-(void)dobutton1
{
    ThdViewController *thd = [[ThdViewController alloc]init];
    [self.navigationController pushViewController:thd animated:YES];
}

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

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
//
//  ThdViewController.m
//  lesson_navgation
//
//  Created by lanouhn on 15-1-8.
//  Copyright (c) 2015年 lanouhn. All rights reserved.
//

#import "ThdViewController.h"

@interface ThdViewController ()

@end

@implementation ThdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.backgroundColor = [UIColor redColor];
    btn.frame=CGRectMake(100,350,120,50);
    [btn setTitle:@"Back " forState:UIControlStateNormal];
    [self.view addSubview:btn];
    [btn addTarget:self action:@selector(dobutton) forControlEvents:UIControlEventTouchDown];
    
    
}
-(void)dobutton
{
    //[self.navigationController popToRootViewControllerAnimated:YES];
    //返回到指定的页面
    //获取当前导航控制器栈里面所有页面的方法
    NSArray *array=[self.navigationController viewControllers];
    //想返回到哪一页就从数组中取到哪一页的索引
    [self.navigationController popToViewController:[array objectAtIndex:0] animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值