首页 > 代码库 > 源码03-02-10-导航控制器简单使用

源码03-02-10-导航控制器简单使用

 

技术分享

////  AppDelegate.m//  10-导航控制器简单使用#import "AppDelegate.h"#import "OneViewController.h"@interface AppDelegate ()@end@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    // Override point for customization after application launch.        // 创建窗口    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];        // 创建导航控制器的跟控制器,也属于导航控制器的子控制器    UIViewController *vc = [[OneViewController alloc] init];    vc.view.backgroundColor = [UIColor redColor];            // 导航控制器也需要一个根控制器    // 默认导航控制器把根控制器的view添加到导航控制器的view上    UINavigationController *navVc = [[UINavigationController alloc] initWithRootViewController:vc];        NSLog(@"%@",navVc);    // 设置窗口的跟控制器    self.window.rootViewController = navVc;        [self.window makeKeyAndVisible];    return YES;}@end

OneViewController.xib

技术分享

////  OneViewController.h//  10-导航控制器简单使用#import <UIKit/UIKit.h>@interface OneViewController : UIViewController@end

 

////  OneViewController.m//  10-导航控制器简单使用#import "OneViewController.h"#import "TwoViewController.h"@interface OneViewController ()@end@implementation OneViewController// 跳转第二个控制器- (IBAction)jump2Two:(id)sender {    //在这里设置要跳去的的控制器    TwoViewController *vc = [[TwoViewController alloc] init];        vc.view.backgroundColor = [UIColor yellowColor];            // 跳转    // 如果导航控制器调用push,就会把vc添加为导航控制器的子控制器    [self.navigationController pushViewController:vc animated:YES];    //    NSLog(@"%@",self.navigationController);        }- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view from its nib.}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end

 

TwoViewCoutroller.xib

技术分享

 

////  TwoViewController.m//  10-导航控制器简单使用#import "TwoViewController.h"#import "ThreeViewController.h"@interface TwoViewController ()@end@implementation TwoViewController- (IBAction)jump2Three:(id)sender {    ThreeViewController *three = [[ThreeViewController alloc] init];        [self.navigationController pushViewController:three animated:YES];    }- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view from its nib.}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end

 ThreeViewController.xib

技术分享

////  ThreeViewController.m//  10-导航控制器简单使用#import "ThreeViewController.h"@interface ThreeViewController ()@end@implementation ThreeViewController// 返回上一个控制器- (IBAction)backToPre:(id)sender {        // pop不是马上把控制器销毁,    // 回到上一个界面    [self.navigationController popViewControllerAnimated:YES];    }// 返回到导航控制器的跟控制器- (IBAction)back2Root:(id)sender {        // 注意:只能返回到栈里面的控制器    [self.navigationController popToViewController:self.navigationController.childViewControllers[0] animated:YES];//    [self.navigationController popToRootViewControllerAnimated:YES];}- (void)dealloc{    NSLog(@"%s",__func__);}- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view from its nib.//    NSLog(@"%@",self.navigationController.viewControllers);        NSLog(@"%@",self.navigationController.topViewController);}- (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

 

源码03-02-10-导航控制器简单使用