码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2014-08-01 00:08:40      阅读:402      评论:0      收藏:0      [点我收藏+]

标签:ios   navigationcontroller   push   数据传递   

最终效果图:

bubuko.com,布布扣


storyboard示意图:

bubuko.com,布布扣


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方式_传递数据,布布扣,bubuko.com

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

标签:ios   navigationcontroller   push   数据传递   

原文地址:http://blog.csdn.net/pre_eminent/article/details/38323447

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!