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

最终效果图:



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左侧的按钮
@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



iOS_18_控制器切换_NavigationController_push方式_传递数据,,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。