首页 > 代码库 > UINavigationController页面切换合基本设置
UINavigationController页面切换合基本设置
新建的类和文件名
AppDelegate.h
#import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @end
AppDelegate.m
#import "AppDelegate.h" #import "FirstViewController.h" #import "SecondViewController.h" #import "ThirdViewController.h" #import "ForthViewController.h" @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]; FirstViewController *firstVC = [[FirstViewController alloc]init]; //导航视图控制器的使用 (UINavigationController) //创建的时候,要给他指定一个默认显示的viewController UINavigationController *naviVC = [[UINavigationController alloc] initWithRootViewController:firstVC]; //把navigationController指定为window的根视图控制器 self.window.rootViewController = naviVC; [firstVC release]; [_window release]; return YES; } - (void)dealloc { [_window release]; [super dealloc]; } - (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.h
#import <UIKit/UIKit.h> @interface FirstViewController : UIViewController @end
FirstViewController.m
#import "FirstViewController.h" #import "SecondViewController.h" @interface FirstViewController () @end @implementation FirstViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor = [UIColor brownColor]; self.view.alpha = 0.3; self.title = @"第一页面"; //1.改变navigationBar的样式 // 一个导航控制器只有一个navigationBar //导航栏是否有一个模糊效果(默认为YES) [self.navigationController.navigationBar setTranslucent:YES]; //导航栏颜色 // [self.navigationController.navigationBar setBackgroundColor:[UIColor blueColor]]; [self.navigationController.navigationBar setBarTintColor:[UIColor yellowColor]]; //隐藏导航栏 [self.navigationController setNavigationBarHidden:NO animated:YES]; //navigationItem 作用:每个视图控制器要显示在导航栏上的内容 //当导航控制器推出一个新的vc的时候,会读取这个属性的所有内容显示到导航栏上 self.navigationItem.title = @"First"; //设置左右两边的按钮 self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemOrganize target:self action:@selector(buttonClicked:)]; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"hehe" style:UIBarButtonItemStylePlain target:self action:@selector(buttonClicked:) ]; //返回字体颜色 [self.navigationController.navigationBar setTintColor:[UIColor redColor]]; UIButton *botton = [[UIButton alloc] initWithFrame:CGRectMake(80, 100, 90, 30)]; botton.backgroundColor = [UIColor cyanColor]; [botton setTitle:@"下页面" forState:UIControlStateNormal]; [botton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [botton setShowsTouchWhenHighlighted:YES]; botton.layer.cornerRadius = 5; [botton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:botton]; } - (void)buttonClicked:(UIButton *)button { //利用navigetion推出新页面 //1.创建一个新的viewController SecondViewController *secondVC = [[SecondViewController alloc] init]; //2.把页面推出 secondVC.name = button.currentTitle; //可以通过属性直接获取当前的viewController所在的导航控制器 [self.navigationController pushViewController:secondVC animated:YES]; //3.内存管理 [secondVC release]; } - (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
SecondViewController.h
#import <UIKit/UIKit.h> @interface SecondViewController : UIViewController @property (nonatomic, copy)NSString *name; @end
SecondViewController.m
#import "SecondViewController.h" #import "ThirdViewController.h" @interface SecondViewController () @end @implementation SecondViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor = [UIColor blueColor]; // self.view.alpha = 0.3; self.title = @"第二页面"; UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(110, 100, 90, 30)]; [button setTitle:self.name forState:UIControlStateNormal]; [button setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal]; button.backgroundColor = [UIColor redColor]; button.layer.cornerRadius = 5; [button addTarget:self action:@selector(buttonClikded:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; // UIImage *image = [UIImage imageNamed:@"4.png"]; self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:NULL] autorelease]; UISegmentedControl *segment = [[UISegmentedControl alloc] initWithItems:@[@"红茶", @"绿茶"]]; self.navigationItem.titleView = segment; [segment release]; [self.navigationController.navigationBar setTintColor:[UIColor blueColor]]; } - (void)buttonClikded:(UIButton *)button { ThirdViewController *thirdVC = [[ThirdViewController alloc] init]; [self.navigationController pushViewController:thirdVC animated:YES]; [thirdVC release]; } - (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
ThirdViewController.h
#import <UIKit/UIKit.h> @interface ThirdViewController : UIViewController @end
ThirdViewController.m
#import "ThirdViewController.h" #import "ForthViewController.h" @interface ThirdViewController () @end @implementation ThirdViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor = [UIColor magentaColor]; self.title = @"第三页"; UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(150, 100, 90, 30)]; [button setTitle:@"下个页面" forState:UIControlStateNormal]; button.backgroundColor = [UIColor cyanColor]; button.layer.cornerRadius = 5; [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"3.png"] style:UIBarButtonItemStylePlain target:self action:NULL]; } - (void)buttonClicked:(UIButton *)button { ForthViewController *forthVC = [[ForthViewController alloc] init]; [self.navigationController pushViewController:forthVC animated:YES]; [forthVC release]; } - (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
第四个页面和第三个页面相同
本文出自 “小刘_Blog” 博客,请务必保留此出处http://liuyafang.blog.51cto.com/8837978/1548986
UINavigationController页面切换合基本设置
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。