1. 程式人生 > >控制器之間的資料傳遞——屬性傳值

控制器之間的資料傳遞——屬性傳值

屬性傳值

我們先在這裡約定:介面1傳值到介面2為順傳,介面2傳值到介面1為逆傳

一. 順傳

  1. 介面2需要有一個用於接收資料的屬性
  2. 介面2在壓入棧之前,把介面1中的資料直接賦值給介面2的屬性
  3. 介面2壓入棧,實現跳轉到介面2

二. 逆傳

  1. 介面1需要有一個用於接收資料的屬性
  2. 介面2需要有一個用於儲存介面1的屬性
  3. 介面2在壓入棧之前,把介面1控制器賦值給介面2的屬性
  4. 在介面2中,通過儲存的介面1,可以把需要傳的值給介面1
  5. 介面2出棧,跳轉到介面1

三. 具體實現

1. AppDelegate類

#import "AppDelegate.h"
#include "OneViewController.h"
@interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // 1. 建立視窗 self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; // 2. 建立視窗的根控制器 // 2.1 建立導航控制器的根控制器
UIViewController *oneVc = [[OneViewController alloc] init]; // 2.2 建立導航控制器 UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:oneVc]; // 2.3 給視窗設定根控制器 self.window.rootViewController = nav; // 3. 設定視窗為主視窗並顯示視窗 [self.window makeKeyAndVisible]; // 隱藏導航控制器的導航條
nav.navigationBarHidden = YES; return YES; } @end

2. OneViewController類

---------- OneViewController.h檔案

#import <UIKit/UIKit.h>

@interface OneViewController : UIViewController

// 用於儲存資料
@property (nonatomic,strong) NSString *oneStr;

@end

---------- OneViewController.m檔案

#import "OneViewController.h"
#import "TwoViewController.h"

@interface OneViewController ()

@property (nonatomic,strong) UITextField *textField;

@end

@implementation OneViewController

- (void)viewDidLoad {
  [super viewDidLoad];

  //設定控制器View的背景顏色
  self.view.backgroundColor = [UIColor greenColor];

  // 建立點選按鈕
  UIButton *clickBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  clickBtn.frame = CGRectMake(10, 100, 80, 40);
  clickBtn.backgroundColor = [UIColor whiteColor];
  [clickBtn setTitle:@"到介面2" forState:UIControlStateNormal];
  [clickBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  [clickBtn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
  [clickBtn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
  [self.view addSubview:clickBtn];

  // 建立文字框
  _textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 150, 250, 40)];
  _textField.borderStyle = UITextBorderStyleRoundedRect;
  _textField.backgroundColor = [UIColor whiteColor];
  [self.view addSubview:_textField];

}

#pragma mark - 點選事件

- (void)btnClick {

  // 建立介面2
  TwoViewController *twoVc = [[TwoViewController alloc] init];
  // 把自己賦值給要跳轉控制器的屬性,用於資料的逆傳
  twoVc.oneVC = self;
  // 把文字框的值賦值給twoVc控制器
  twoVc.twoStr = _textField.text;

  // twoVc控制器壓入棧,顯示該介面2
  [self.navigationController pushViewController:twoVc animated:YES];

}

- (void)viewWillAppear:(BOOL)animated {

  _textField.text = _oneStr;

}

@end

3. TwoViewController

---------- TwoViewController.h檔案

#import <UIKit/UIKit.h>
@class OneViewController;

@interface TwoViewController : UIViewController

// 用於儲存資料
@property (nonatomic,strong) NSString *twoStr;

// 儲存前一個控制器
@property (nonatomic,strong) OneViewController *oneVC;

@end

---------- TwoViewController.m檔案

#import "TwoViewController.h"
#import "OneViewController.h"

@interface TwoViewController ()

@property (nonatomic,strong) UITextField *textField;

@end

@implementation TwoViewController

- (void)viewDidLoad {
    [super viewDidLoad];

  //設定控制器View的背景顏色
  self.view.backgroundColor = [UIColor yellowColor];

  // 建立點選按鈕
  UIButton *clickBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  clickBtn.frame = CGRectMake(10, 100, 80, 40);
  clickBtn.backgroundColor = [UIColor whiteColor];
  [clickBtn setTitle:@"傳 值" forState:UIControlStateNormal];
  [clickBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  [clickBtn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
  [clickBtn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
  [self.view addSubview:clickBtn];

  // 建立文字框
  _textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 150, 250, 40)];
  _textField.borderStyle = UITextBorderStyleRoundedRect;
  _textField.backgroundColor = [UIColor whiteColor];
  [self.view addSubview:_textField];
}

#pragma mark - 點選事件

- (void)btnClick {

  // 給介面1傳遞資料
  _oneVC.oneStr = _textField.text;

  // 跳轉介面
  [self.navigationController popViewControllerAnimated:YES];

}

- (void)viewWillAppear:(BOOL)animated {

  _textField.text = _twoStr;

}

@end