你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS_18_控制器切換_NavigationController_push方式_傳遞數據

iOS_18_控制器切換_NavigationController_push方式_傳遞數據

編輯:IOS開發綜合

最終效果圖:

\


storyboard示意圖:<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+PGltZyBzcmM9"/uploadfile/Collfiles/20140801/201408010846099.png" alt="\">


BeyondViewController.h

//
//  BeyondViewController.h
//  18_控制器切換_navigation_push_通過storyboard方式
//
//  Created by beyond on 14-7-31.
//  Copyright (c) 2014年 com.beyond. All rights reserved.
//

#import 

@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 

@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 

@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



  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved