最終效果圖:php
storyboard示意圖:java
BeyondViewController.happ
//// BeyondViewController.h// 18_控制器切換_navigation_push_經過storyboard方式//// Created by beyond on 14-7-31.// Copyright (c) 2014年 com.beyond. All rights reserved.//#import <UIKit/UIKit.h>@interface BeyondViewController : UIViewController// NavigationItem左側的按鈕@property (weak, nonatomic) IBOutlet UIBarButtonItem *refreshBtn;// NavigationItem右側的按鈕@property (weak, nonatomic) IBOutlet UIBarButtonItem *wantToLoginBtn;// NavigationItem左側的按鈕 點擊事件 刷新至初始狀態- (IBAction)refresh:(UIBarButtonItem *)sender;@end
BeyondViewController.mide
//// BeyondViewController.m// 18_控制器切換_navigation_push_經過storyboard方式//// Created by beyond on 14-7-31.// Copyright (c) 2014年 com.beyond. All rights reserved.//#import "BeyondViewController.h"@interface BeyondViewController ()@end@implementation BeyondViewController// 刷新至初始狀態- (IBAction)refresh:(UIBarButtonItem *)sender { self.navigationItem.title = @"首頁"; self.wantToLoginBtn.enabled = YES; self.refreshBtn.enabled = NO; }@end
LoginViewController.h動畫
//// LoginViewController.h// 18_控制器切換_navigation_push_經過storyboard方式//// Created by beyond on 14-7-31.// Copyright (c) 2014年 com.beyond. All rights reserved.//#import <UIKit/UIKit.h>@interface LoginViewController : UIViewController// 用戶名輸入框@property (weak, nonatomic) IBOutlet UITextField *username;// 密碼輸入框@property (weak, nonatomic) IBOutlet UITextField *password;// 輸入用戶名和密碼以後,點擊 登陸按鈕- (IBAction)loginBtnClick:(UIButton *)sender;// NavigationItem左側的按鈕 點擊事件 返回前一個控制器- (IBAction)backToHome:(UIBarButtonItem *)sender;@end
LoginViewController.matom
//// LoginViewController.m// 18_控制器切換_navigation_push_經過storyboard方式//// Created by beyond on 14-7-31.// Copyright (c) 2014年 com.beyond. All rights reserved.//#import "LoginViewController.h"#import "NanaViewController.h"@interface LoginViewController () @end @implementation LoginViewController - (void)viewDidLoad { [super viewDidLoad]; _password.secureTextEntry = YES; } - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; }#pragma mark - Navigation// 在經過segue跳轉至下一個導航子控制器前,作準備工做!這兒是傳遞數據給目的控制器- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)username { // 要獲得目的控制器,請使用 [segue destinationViewController]. // 在這裏,能夠傳遞數據給下一個控制器 // 這個參數是要傳遞的數據 NSLog(@"prepare for segue----%@",username); // 經過segue的destinationViewController獲得即將要跳轉的目的控制器,傳遞數據給它 NanaViewController *nanaVC = [segue destinationViewController]; NSString *oldTitle = nanaVC.item_nanaSay.title; nanaVC.username = username; NSString *newStr = [NSString stringWithFormat:@"%@你好呀~%@ ",username,oldTitle]; nanaVC.item_nanaSay.title = newStr; }// 輸入用戶名和密碼以後,點擊 登陸按鈕- (IBAction)loginBtnClick:(UIButton *)sender { // robust判斷 if (_username.text.length == 0 || _password.text.length == 0) { UIActionSheet *sheet = [[UIActionSheet alloc]initWithTitle:@"請輸入賬號和密碼" delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:@"明白" otherButtonTitles:@"other", nil]; [sheet showInView:self.view]; [_username becomeFirstResponder]; return; } // 輸入正確的密碼和帳號以後,跳轉至第3個控制器// self.navigationController pushViewController:<#(UIViewController *)#> animated:<#(BOOL)#> // 經過segue連線,跳至self.navigationController容器裏面的下一個 子控制器,而且傳遞參數(用戶名),參數會被傳遞到self的 prepareForSegue方法中,而後纔會傳遞到 下一下控制器(destination) [self performSegueWithIdentifier:@"segue_loginSuccess" sender:_username.text]; }// NavigationItem左側的按鈕 點擊事件 返回前一個控制器,即首頁- (IBAction)backToHome:(UIBarButtonItem *)sender { [self.navigationController popViewControllerAnimated:YES]; } @end
NanaViewController.hspa
//// NanaViewController.h// 18_控制器切換_navigation_push_經過storyboard方式//// Created by beyond on 14-7-31.// Copyright (c) 2014年 com.beyond. All rights reserved.//#import <UIKit/UIKit.h>@interface NanaViewController : UIViewController// NavigationItem右側的按鈕 歡迎 標語@property (weak, nonatomic) IBOutlet UIBarButtonItem *item_nanaSay;// 點擊NavigationItem左側的按鈕 回到首頁,即第一個控制器,而且將數據帶過去- (IBAction)backToHome:(UIBarButtonItem *)sender;// 僅用來接收傳遞過來的數據用~@property (nonatomic,copy) NSString * username;@end
NanaViewController.m.net
//// NanaViewController.m// 18_控制器切換_navigation_push_經過storyboard方式//// Created by beyond on 14-7-31.// Copyright (c) 2014年 com.beyond. All rights reserved.//#import "NanaViewController.h"#import "BeyondViewController.h"@interface NanaViewController () @end @implementation NanaViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; }// 回到首頁,即第一個控制器,而且將數據帶過去- (IBAction)backToHome:(UIBarButtonItem *)sender { // 拿到第一個控制器 BeyondViewController *firstVC = [self.navigationController.viewControllers firstObject]; // [self.navigationController.viewControllers objectAtIndex:n-2]; //n為最頂的index //加入要傳遞的數據 NSString *str = [NSString stringWithFormat:@"歡迎%@回來",_username]; firstVC.navigationItem.title = str; firstVC.wantToLoginBtn.enabled = NO; firstVC.refreshBtn.enabled = YES; // pop至第一個控制器 [self.navigationController popToViewController:firstVC animated:YES]; } @end
========================3d
**************代理
我是分割線
**************
=================================
push的實現,使用代碼方式:
BeyondAppDelegate.h
//// BeyondAppDelegate.h// 18_控制器切換_navigationCtrl_push_代碼方式//// Created by beyond on 14-7-31.// Copyright (c) 2014年 com.beyond. All rights reserved.//#import <UIKit/UIKit.h>@class BeyondViewController;@interface BeyondAppDelegate : UIResponder <UIApplicationDelegate>@property (strong, nonatomic) UIWindow *window;// 爲應用程序的代理 添加一個成員屬性 控制器@property (nonatomic,strong) BeyondViewController *viewController;@end
BeyondAppDelegate.m
// // BeyondAppDelegate.m // 18_控制器切換_navigationCtrl_push_代碼方式 // // Created by beyond on 14-7-31. // Copyright (c) 2014年 com.beyond. All rights reserved. // #import "BeyondAppDelegate.h" #import "BeyondViewController.h" @implementation BeyondAppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { /* ===============================對比版 self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. // 爲應用代理 的成員屬性 實例化一個BeyondViewController控制器 self.viewController = [[BeyondViewController alloc] initWithNibName:@"BeyondViewController" bundle:nil]; // 將BeyondViewController控制器,設置成爲窗口的rootViewController self.window.rootViewController = self.viewController; self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; return YES; <pre name="code" class="objc">===============================對比版 <span style="font-family: Arial, Helvetica, sans-serif;">*/</span>
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // 爲應用代理 的成員屬性 實例化一個BeyondViewController控制器 self.viewController = [[BeyondViewController alloc] initWithNibName:@"BeyondViewController" bundle:nil]; // 建立導航控制器,並設置 棧底的控制器爲 BeyondViewController UINavigationController *navi = [[UINavigationController alloc]initWithRootViewController:self.viewController]; // 最後將導航 控制器,設置成爲窗口的rootViewController self.window.rootViewController = navi; self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; return YES;}@end
BeyondViewController.m
//// BeyondViewController.m// 18_控制器切換_navigationCtrl_push_代碼方式//// Created by beyond on 14-7-31.// Copyright (c) 2014年 com.beyond. All rights reserved./* 導航控制器方法總結: 1,建立導航控制器,在App代理的方法裏面 // 建立導航控制器,並設置 棧底的控制器爲 BeyondViewController UINavigationController *navi = [[UINavigationController alloc]initWithRootViewController:self.viewController]; 2,棧 的形式管理 self.navigationController.viewControllers 3,壓棧 即跳至下一個控制器 [self.navigationController pushViewController:loginVC animated:YES]; 4,出棧 即返回,將控制器從stack裏面彈出 [self.navigationController popToRootViewControllerAnimated:YES]; [self.navigationController popViewControllerAnimated:YES]; [self.navigationController popToViewController:<#(UIViewController *)#> animated:<#(BOOL)#>]; 5,導航欄顯示標題,左Item,右item,返回item 其中 返回的文本是由上一個控制器決定 的 ??? self.title 就是 self.navigationItem.title 6,取得棧頂控制器 self.navigationController.topViewController */#import "BeyondViewController.h"#import "LoginViewController.h"@interface BeyondViewController ()@end@implementation BeyondViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // 設置頂部標題 self.navigationItem.title = @"首頁"; // 設置右邊的按鈕和點擊事件 self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"下一張" style:UIBarButtonItemStyleBordered target:self action:@selector(nextBtnClick)]; }// 點擊 下一張,進入 下一個控制器- (void)nextBtnClick { // 實例化,下一個控制器,並push添加到父容器中,並動畫跳轉 LoginViewController *loginVC = [[LoginViewController alloc]init]; // 這樣子也能夠拿到容器控制器,即導航控制器[self.parentViewController]; [self.navigationController pushViewController:loginVC animated:YES]; }@end
BeyondViewController.xib
第2個控制器.m
//// LoginViewController.m// 18_控制器切換_navigationCtrl_push_代碼方式//// Created by beyond on 14-7-31.// Copyright (c) 2014年 com.beyond. All rights reserved.//#import "LoginViewController.h"#import "NanaViewController.h"@interface LoginViewController ()@end@implementation LoginViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // 設置頂部標題 self.navigationItem.title = @"料理"; // 設置返回按鈕的顯示 文本 self.navigationItem.backBarButtonItem.title = @"上一張"; //設置頂部 右邊的按鈕,並添加監聽事件 UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithTitle:@"下一張" style:UIBarButtonItemStyleBordered target:self action:@selector(rightBtnClick)]; self.navigationItem.rightBarButtonItem = item; }// 響應 頂部 右邊的 點擊- (void)rightBtnClick { // 實例化,第3個控制器,並push至棧頂 // 實例化,下一個控制器,並push添加到父容器中,並動畫跳轉 NanaViewController *nanaVC = [[NanaViewController alloc]init]; // 這樣子也能夠拿到容器控制器,即導航控制器[self.parentViewController]; [self.navigationController pushViewController:nanaVC animated:YES]; }@end