最后效果图:
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcHJlX2VtaW5lbnQ=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">
storyboard示意图:
BeyondViewController.h
//
// 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左側的button
@property (weak, nonatomic) IBOutlet UIBarButtonItem *refreshBtn;
// NavigationItem右側的button
@property (weak, nonatomic) IBOutlet UIBarButtonItem *wantToLoginBtn; // NavigationItem左側的button 点击事件 刷新至初始状态
- (IBAction)refresh:(UIBarButtonItem *)sender; @end
BeyondViewController.m
//
// 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 // username输入框
@property (weak, nonatomic) IBOutlet UITextField *username;
// password输入框
@property (weak, nonatomic) IBOutlet UITextField *password; // 输入username和password之后,点击 登录button
- (IBAction)loginBtnClick:(UIButton *)sender; // NavigationItem左側的button 点击事件 返回前一个控制器
- (IBAction)backToHome:(UIBarButtonItem *)sender; @end
LoginViewController.m
//
// 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; } // 输入用户名和password之后,点击 登录button
- (IBAction)loginBtnClick:(UIButton *)sender
{
// robust推断
if (_username.text.length == 0 || _password.text.length == 0) {
UIActionSheet *sheet = [[UIActionSheet alloc]initWithTitle:@"请输入帐号和password" delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:@"明确" otherButtonTitles:@"other", nil];
[sheet showInView:self.view];
[_username becomeFirstResponder];
return;
} // 输入正确的password和账号之后,跳转至第3个控制器
// self.navigationController pushViewController:<#(UIViewController *)#> animated:<#(BOOL)#> // 通过segue连线,跳至self.navigationController容器里面的下一个 子控制器,而且传递參数(用户名),參数会被传递到self的 prepareForSegue方法中,然后才会传递到 下一下控制器(destination)
[self performSegueWithIdentifier:@"segue_loginSuccess" sender:_username.text]; } // NavigationItem左側的button 点击事件 返回前一个控制器,即首页
- (IBAction)backToHome:(UIBarButtonItem *)sender
{
[self.navigationController popViewControllerAnimated:YES]; }
@end
NanaViewController.h
//
// 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右側的button 欢迎 标语
@property (weak, nonatomic) IBOutlet UIBarButtonItem *item_nanaSay; // 点击NavigationItem左側的button 回到首页,即第一个控制器,而且将数据带过去
- (IBAction)backToHome:(UIBarButtonItem *)sender; // 仅用来接收传递过来的数据用~
@property (nonatomic,copy) NSString * username;
@end
NanaViewController.m
//
// 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
========================
**************
我是切割线
**************
=================================
push的实现,使用代码方式:
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcHJlX2VtaW5lbnQ=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">
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; ===============================对照 原版 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
<pre>
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 = @"首页";
// 设置右边的button和点击事件 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 = @"料理"; // 设置返回button的显示 文本
self.navigationItem.backBarButtonItem.title = @"上一张"; //设置顶部 右边的button,并加入监听事件
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
版权声明:本文博客原创文章。博客,未经同意,不得转载。