iOS屬性傳值、代理傳值

import "AppDelegate.h"

import "FirstViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];

    FirstViewController *firstVC = [[FirstViewController alloc]init];
    UINavigationController *navC = [[UINavigationController alloc]initWithRootViewController:firstVC];
    [self.window setRootViewController:navC];
    return YES;
    }


import "FirstViewController.h"

import "SecondViewController.h"

@interface FirstViewController ()

@end

@implementation FirstViewController

  • (void)viewDidLoad {
    [super viewDidLoad];
    // 設置標題
    self.navigationItem.title = @"FirstVC";
    // 設置導航條右側按鈕
    UIBarButtonItem *rightBtnItem = [[UIBarButtonItem alloc]initWithTitle:@"下一步" style:UIBarButtonItemStyleDone target:self action:@selector(rightBarButtonAction:)];
    self.navigationItem.rightBarButtonItem = rightBtnItem;

    // 設置導航左側按鈕(點擊后回收鍵盤)
    UIBarButtonItem *leftBtnItem = [[UIBarButtonItem alloc]initWithTitle:@"回收鍵盤" style:UIBarButtonItemStyleDone target:self action:@selector(endEditingAction)];
    leftBtnItem.tag = 2000;
    self.navigationItem.leftBarButtonItem = leftBtnItem;

    // 初始化一個textField文本輸入框
    UITextField *userTextField = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 200, 40)];
    userTextField.borderStyle = UITextBorderStyleBezel;
    userTextField.textAlignment = NSTextAlignmentCenter;
    userTextField.placeholder = @"請輸入文字";
    userTextField.tag = 1000;
    [self.view addSubview:userTextField];

    UITextField *pwsTextField = [[UITextField alloc]initWithFrame:CGRectMake(100, 180, 200, 40)];
    pwsTextField.borderStyle = UITextBorderStyleBezel;
    pwsTextField.textAlignment = NSTextAlignmentCenter;
    pwsTextField.placeholder = @"密 碼";
    pwsTextField.keyboardType = UIKeyboardTypeNumberPad;
    pwsTextField.secureTextEntry = YES;
    pwsTextField.tag = 1001;
    [self.view addSubview:pwsTextField];

    UITextField *yanZhengTextField = [[UITextField alloc]initWithFrame:CGRectMake(100, 260, 200, 40)];
    

    yanZhengTextField.borderStyle = UITextBorderStyleBezel;
    yanZhengTextField.textAlignment = NSTextAlignmentCenter;
    yanZhengTextField.keyboardType = UIKeyboardTypeNumberPad;
    pwsTextField.keyboardType = UIKeyboardTypeNumberPad;
    yanZhengTextField.placeholder = @"請輸入驗證碼";
    yanZhengTextField.tag = 1002;
    [self.view addSubview:yanZhengTextField];

    }
    // 導航條右側按鈕回調方法,具體實現為推送到下個界面
    -(void)rightBarButtonAction:(UIBarButtonItem*)sender{
    SecondViewController *secondVC = [[SecondViewController alloc]init];
    // 將文本輸入框的值賦值secondVC的屬性
    UITextField userTextField = (UITextField)[self.view viewWithTag:1000];// textField為局部變量
    secondVC.userStr = userTextField.text;
    UITextField pwsTextField = (UITextField)[self.view viewWithTag:1001];
    secondVC.pwsStr = pwsTextField.text;
    UITextField yanZhengTextField = (UITextField)[self.view viewWithTag:1002];
    secondVC.yanZhengStr = yanZhengTextField.text;

    [self.navigationController pushViewController:secondVC animated:YES];
    }

// 導航條左側按鈕回調方法
-(void)endEditingAction{
[self.view endEditing:YES];
}

  • (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }

import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController

@property (nonatomic ,retain)NSString *userStr; // 用來接受第一個界面傳過來的內容
@property (nonatomic ,retain)NSString *pwsStr;
@property (nonatomic ,retain)NSString *yanZhengStr;

@end

import "SecondViewController.h"

import "ThirdViewController.h"

@interface SecondViewController ()<ThirdViewControllerDelegate>

@end

@implementation SecondViewController

// thirdVC的代理方法,作用為,將thirdVC上的值傳遞到當前的視圖控制器
-(void)passValueWithDic:(NSDictionary *)valueDic{
NSLog(@"dic_______%@",valueDic);
UILabel userLabel = (UILabel)[self.view viewWithTag:5000];
userLabel.text = [valueDic valueForKey:@"name"];

UILabel *pswLabel = (UILabel*)[self.view viewWithTag:5001];
pswLabel.text = [valueDic valueForKey:@"pws"];

UILabel *yanZhengLabel = [self.view viewWithTag:5002];
yanZhengLabel.text = [valueDic valueForKey:@"yanZheng"];

}

  • (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"SecondVC";
    // 聲明一個label,用來顯示首個界面傳過來的內容
    UILabel *userLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 40)];
    // label顯示的內容為從首個界面傳過來的值
    userLabel.text = self.userStr;
    userLabel.tag = 5000;
    [self.view addSubview:userLabel];

    UILabel *pwsLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 180, 200, 40)];
    pwsLabel.text = self.pwsStr;
    pwsLabel.tag = 5001;
    [self.view addSubview:pwsLabel];

    UILabel *yanZhengLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 260, 200, 40)];
    yanZhengLabel.text = self.yanZhengStr;
    yanZhengLabel.tag = 5002;
    [self.view addSubview:yanZhengLabel];

    // 設置導航條右側按鈕,點擊跳轉到下個界面(ThirdViewController)
    UIBarButtonItem *rightBtnItem = [[UIBarButtonItem alloc]initWithTitle:@"下一步" style:UIBarButtonItemStyleDone target:self action:@selector(rightBarButtonAction:)];
    self.navigationItem.rightBarButtonItem = rightBtnItem;
    }

// 導航條右側按鈕回調方法,點擊跳轉到下個界面(ThirdViewController)
-(void)rightBarButtonAction:(UIBarButtonItem*)sender{
ThirdViewController *thirdVC = [[ThirdViewController alloc]init];
// 指定代理 {self:在類方法(加號方法)中指的是本類,在對象方法(減號方法)中指的是該類的一個對象}
thirdVC.delegate = self;
[self.navigationController pushViewController:thirdVC animated:YES];
}

  • (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }
    @end

import <UIKit/UIKit.h>

@protocol ThirdViewControllerDelegate <NSObject>

// 該方法的參數就是要傳遞的值
-(void)passValueWithDic:(NSDictionary*)valueDic;

@end

@interface ThirdViewController : UIViewController

@property (nonatomic ,assign)id<ThirdViewControllerDelegate> delegate;

@end

import "ThirdViewController.h"

@interface ThirdViewController ()

@end

@implementation ThirdViewController

  • (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"ThirdVC";

    for (int i = 0; i < 3; i++) {
    UITextField textField = [[UITextField alloc]initWithFrame:CGRectMake(100, 100+100i, 200, 40)];
    textField.borderStyle = UITextBorderStyleBezel;
    textField.textAlignment = NSTextAlignmentCenter;
    textField.placeholder = @"請輸入文字";
    textField.tag = 1000+i;
    [self.view addSubview:textField];
    }

    // 因為需要向前傳值,所以需要捕獲點擊按鈕的事件,所以需要重新定義leftBarButtonItem
    UIBarButtonItem *leftBtnItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:@selector(backBtnAction:)];
    leftBtnItem.tag = 2000;
    self.navigationItem.leftBarButtonItem = leftBtnItem;

}
// 返回按鈕的點擊事件
-(void)backBtnAction:(UIBarButtonItem*)sender{
UITextField nameTextField = (UITextField)[self.view viewWithTag:1000];
UITextField pwsTextField = (UITextField)[self.view viewWithTag:1001];
UITextField yanZhengTextField = (UITextField)[self.view viewWithTag:1002];

/*
 if條件判斷中
 第一步:先判斷是否指定了代理
 第二步:respondsToSelector該方法的返回值為BOOL類型。該方法會從指定的代理類中(這里我們的代理類就是SecondViewController)找尋方法選擇器重中方法的實現,如果沒有實現該協議方法,就返回NO,如果實現,就返回YES
 self.delegate : 指定哪個類為代理,它就是代理類的一個對象,在這里它指的就是 SecondViewController這個類的一個對象那,我們在SecondViewController也實現了passValueWithDic:這個方法,所以我們可以調用此方法
 (self.delegate = SecondViewController)
      */

if (self.delegate && [self.delegate respondsToSelector:@selector(passValueWithDic:)]) {
[self.delegate passValueWithDic:@{@"name":nameTextField.text,@"pws":pwsTextField.text,@"yanZheng":yanZhengTextField.text}];
}

[self.navigationController popViewControllerAnimated:YES];

}

  • (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }
    @end
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,739評論 6 534
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,634評論 3 419
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,653評論 0 377
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,063評論 1 314
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,835評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,235評論 1 324
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,315評論 3 442
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,459評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,000評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,819評論 3 355
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,004評論 1 370
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,560評論 5 362
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,257評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,676評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,937評論 1 288
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,717評論 3 393
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,003評論 2 374

推薦閱讀更多精彩內容

  • { 11、核心動畫 需要簽協議,但是系統幫簽好 一、CABasicAnimation 1、創建基礎動畫對象 CAB...
    CYC666閱讀 1,572評論 2 4
  • //設置尺寸為屏幕尺寸的時候self.window = [[UIWindow alloc] initWithFra...
    LuckTime閱讀 830評論 0 0
  • iOS開發系列--網絡開發 概覽 大部分應用程序都或多或少會牽扯到網絡開發,例如說新浪微博、微信等,這些應用本身可...
    lichengjin閱讀 3,700評論 2 7
  • 第一次在簡書寫東西,表示有點緊張啊。我就想把簡書當作可以傾述的地方,可以寫文章的地方。我現在常常思考我們如何可以勇...
    你不應該太帥啊閱讀 189評論 0 0
  • --著:江涵小子 丁環小寶 鳥亦群飛更亦人, 今京君心為一人; 齊心同德創嘉業, 比翼雙飛又是春。
    江涵少年閱讀 106評論 0 3