iOS_18_控制器切換_NavigationController_push方式_傳遞資料

來源:互聯網
上載者:User

iOS_18_控制器切換_NavigationController_push方式_傳遞資料

最終:



storyboard:<喎?http://www.bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+PGltZyBzcmM9"http://www.2cto.com/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



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.