UI基础 导航控制器
app .m
#import "AppDelegate.h" #import "RootViewController.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; RootViewController * root=[[RootViewController alloc]init]; // self.window.rootViewController=root; //创建导航控制器 UINavigationController *nav =[[UINavigationController alloc]initWithRootViewController:root]; //把导航控制器作为window的视图 self.window.rootViewController =nav; //导航栏的颜色 nav.navigationBar.barTintColor=[UIColor greenColor]; //半透明 nav.navigationBar.translucent=NO; return YES; }
root.m
#import "RootViewController.h" #import "SecondViewController.h" @interface RootViewController () @end @implementation RootViewController - (void)viewDidLoad { [super viewDidLoad]; // UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; // // view.backgroundColor=[UIColor redColor]; // [self.view addSubview:view]; // //把控制导航栏内容代码写到对应的页面上来 self.navigationItem.title=@"首页"; // self.navigationItem.titleView= //左右侧的按钮 文字 // self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc] initWithTitle:@"右侧" style:UIBarButtonItemStylePlain target:self action:@selector(touchRight)]; // 图片 self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"Processsettings"] style:UIBarButtonItemStylePlain target:self action:@selector(touchRight)]; } -(void)touchRight { NSLog(@"右侧"); SecondViewController *second =[[SecondViewController alloc]init]; //跳转 [self.navigationController pushViewController:second animated:YES]; } @end
second.m
#import "SecondViewController.h" @interface SecondViewController () @end @implementation SecondViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor =[UIColor greenColor]; // 第二个页面左上角返回按钮 self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"fanhui"] style:UIBarButtonItemStylePlain target:self action:@selector(goBack)]; } -(void)goBack { // 从哪里回到上一个 [self.navigationController popToRootViewControllerAnimated:YES]; } @end