iOS_18_控制器切换_NavigationController_push模式_传递数据

iOS_18_控制器切换_NavigationController_push方式_传递数据

最终效果图:

iOS_18_控制器切换_NavigationController_push模式_传递数据


storyboard示意图:

iOS_18_控制器切换_NavigationController_push模式_传递数据


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左侧的按钮
@property (weak, nonatomic) IBOutlet UIBarButtonItem *refreshBtn;
// NavigationItem右侧的按钮
@property (weak, nonatomic) IBOutlet UIBarButtonItem *wantToLoginBtn;

// NavigationItem左侧的按钮 点击事件 刷新至初始状态
- (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

// 用户名输入框
@property (weak, nonatomic) IBOutlet UITextField *username;
// 密码输入框
@property (weak, nonatomic) IBOutlet UITextField *password;

// 输入用户名和密码之后,点击 登录按钮
- (IBAction)loginBtnClick:(UIButton *)sender;

// NavigationItem左侧的按钮 点击事件  返回前一个控制器
- (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;
    
}



// 输入用户名和密码之后,点击 登录按钮
- (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.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右侧的按钮   欢迎 标语
@property (weak, nonatomic) IBOutlet UIBarButtonItem *item_nanaSay;

// 点击NavigationItem左侧的按钮  回到首页,即第一个控制器,并且将数据带过去
- (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的实现,使用代码方式:

iOS_18_控制器切换_NavigationController_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

iOS_18_控制器切换_NavigationController_push模式_传递数据


第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