您好,登錄后才能下訂單哦!
新建的類和文件名
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 *p_w_picpath = [UIImage p_w_picpathNamed:@"4.png"]; self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithImage:p_w_picpath 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 p_w_picpathNamed:@"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
第四個頁面和第三個頁面相同
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。