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];
    // 設(shè)置標(biāo)題
    self.navigationItem.title = @"FirstVC";
    // 設(shè)置導(dǎo)航條右側(cè)按鈕
    UIBarButtonItem *rightBtnItem = [[UIBarButtonItem alloc]initWithTitle:@"下一步" style:UIBarButtonItemStyleDone target:self action:@selector(rightBarButtonAction:)];
    self.navigationItem.rightBarButtonItem = rightBtnItem;

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

    // 初始化一個(gè)textField文本輸入框
    UITextField *userTextField = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 200, 40)];
    userTextField.borderStyle = UITextBorderStyleBezel;
    userTextField.textAlignment = NSTextAlignmentCenter;
    userTextField.placeholder = @"請(qǐng)輸入文字";
    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 = @"請(qǐng)輸入驗(yàn)證碼";
    yanZhengTextField.tag = 1002;
    [self.view addSubview:yanZhengTextField];

    }
    // 導(dǎo)航條右側(cè)按鈕回調(diào)方法衔瓮,具體實(shí)現(xiàn)為推送到下個(gè)界面
    -(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];
    }

// 導(dǎo)航條左側(cè)按鈕回調(diào)方法
-(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; // 用來(lái)接受第一個(gè)界面?zhèn)鬟^(guò)來(lái)的內(nèi)容
@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上的值傳遞到當(dāng)前的視圖控制器
-(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";
    // 聲明一個(gè)label壁晒,用來(lái)顯示首個(gè)界面?zhèn)鬟^(guò)來(lái)的內(nèi)容
    UILabel *userLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 40)];
    // label顯示的內(nèi)容為從首個(gè)界面?zhèn)鬟^(guò)來(lái)的值
    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];

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

// 導(dǎo)航條右側(cè)按鈕回調(diào)方法疗琉,點(diǎn)擊跳轉(zhuǎn)到下個(gè)界面(ThirdViewController)
-(void)rightBarButtonAction:(UIBarButtonItem*)sender{
ThirdViewController *thirdVC = [[ThirdViewController alloc]init];
// 指定代理 {self:在類方法(加號(hào)方法)中指的是本類冈欢,在對(duì)象方法(減號(hào)方法)中指的是該類的一個(gè)對(duì)象}
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>

// 該方法的參數(shù)就是要傳遞的值
-(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 = @"請(qǐng)輸入文字";
    textField.tag = 1000+i;
    [self.view addSubview:textField];
    }

    // 因?yàn)樾枰蚯皞髦担孕枰东@點(diǎn)擊按鈕的事件没炒,所以需要重新定義leftBarButtonItem
    UIBarButtonItem *leftBtnItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:@selector(backBtnAction:)];
    leftBtnItem.tag = 2000;
    self.navigationItem.leftBarButtonItem = leftBtnItem;

}
// 返回按鈕的點(diǎn)擊事件
-(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類型涛癌。該方法會(huì)從指定的代理類中(這里我們的代理類就是SecondViewController)找尋方法選擇器重中方法的實(shí)現(xiàn)犯戏,如果沒(méi)有實(shí)現(xiàn)該協(xié)議方法送火,就返回NO,如果實(shí)現(xiàn)先匪,就返回YES
 self.delegate : 指定哪個(gè)類為代理种吸,它就是代理類的一個(gè)對(duì)象,在這里它指的就是 SecondViewController這個(gè)類的一個(gè)對(duì)象那呀非,我們?cè)赟econdViewController也實(shí)現(xiàn)了passValueWithDic:這個(gè)方法坚俗,所以我們可以調(diào)用此方法
 (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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市岸裙,隨后出現(xiàn)的幾起案子猖败,更是在濱河造成了極大的恐慌,老刑警劉巖降允,帶你破解...
    沈念sama閱讀 216,997評(píng)論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件恩闻,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡剧董,警方通過(guò)查閱死者的電腦和手機(jī)幢尚,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,603評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門(mén)破停,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人尉剩,你說(shuō)我怎么就攤上這事真慢。” “怎么了理茎?”我有些...
    開(kāi)封第一講書(shū)人閱讀 163,359評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵黑界,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我功蜓,道長(zhǎng)园爷,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,309評(píng)論 1 292
  • 正文 為了忘掉前任式撼,我火速辦了婚禮童社,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘著隆。我一直安慰自己扰楼,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,346評(píng)論 6 390
  • 文/花漫 我一把揭開(kāi)白布美浦。 她就那樣靜靜地躺著弦赖,像睡著了一般。 火紅的嫁衣襯著肌膚如雪浦辨。 梳的紋絲不亂的頭發(fā)上蹬竖,一...
    開(kāi)封第一講書(shū)人閱讀 51,258評(píng)論 1 300
  • 那天,我揣著相機(jī)與錄音流酬,去河邊找鬼币厕。 笑死,一個(gè)胖子當(dāng)著我的面吹牛芽腾,可吹牛的內(nèi)容都是我干的旦装。 我是一名探鬼主播,決...
    沈念sama閱讀 40,122評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼摊滔,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼阴绢!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起艰躺,我...
    開(kāi)封第一講書(shū)人閱讀 38,970評(píng)論 0 275
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤呻袭,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后腺兴,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體左电,經(jīng)...
    沈念sama閱讀 45,403評(píng)論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,596評(píng)論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了券腔。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片伏穆。...
    茶點(diǎn)故事閱讀 39,769評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖纷纫,靈堂內(nèi)的尸體忽然破棺而出枕扫,到底是詐尸還是另有隱情,我是刑警寧澤辱魁,帶...
    沈念sama閱讀 35,464評(píng)論 5 344
  • 正文 年R本政府宣布烟瞧,位于F島的核電站,受9級(jí)特大地震影響染簇,放射性物質(zhì)發(fā)生泄漏参滴。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,075評(píng)論 3 327
  • 文/蒙蒙 一锻弓、第九天 我趴在偏房一處隱蔽的房頂上張望砾赔。 院中可真熱鬧,春花似錦青灼、人聲如沸暴心。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,705評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)专普。三九已至,卻和暖如春弹沽,著一層夾襖步出監(jiān)牢的瞬間檀夹,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,848評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工策橘, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留炸渡,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,831評(píng)論 2 370
  • 正文 我出身青樓役纹,卻偏偏與公主長(zhǎng)得像偶摔,于是被迫代替她去往敵國(guó)和親暇唾。 傳聞我的和親對(duì)象是個(gè)殘疾皇子促脉,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,678評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容

  • { 11、核心動(dòng)畫(huà) 需要簽協(xié)議策州,但是系統(tǒng)幫簽好 一瘸味、CABasicAnimation 1、創(chuàng)建基礎(chǔ)動(dòng)畫(huà)對(duì)象 CAB...
    CYC666閱讀 1,545評(píng)論 2 4
  • //設(shè)置尺寸為屏幕尺寸的時(shí)候self.window = [[UIWindow alloc] initWithFra...
    LuckTime閱讀 809評(píng)論 0 0
  • iOS開(kāi)發(fā)系列--網(wǎng)絡(luò)開(kāi)發(fā) 概覽 大部分應(yīng)用程序都或多或少會(huì)牽扯到網(wǎng)絡(luò)開(kāi)發(fā)够挂,例如說(shuō)新浪微博旁仿、微信等,這些應(yīng)用本身可...
    lichengjin閱讀 3,658評(píng)論 2 7
  • 第一次在簡(jiǎn)書(shū)寫(xiě)東西,表示有點(diǎn)緊張啊枯冈。我就想把簡(jiǎn)書(shū)當(dāng)作可以傾述的地方毅贮,可以寫(xiě)文章的地方。我現(xiàn)在常常思考我們?nèi)绾慰梢杂?..
    你不應(yīng)該太帥啊閱讀 187評(píng)論 0 0
  • --著:江涵小子 丁環(huán)小寶 鳥(niǎo)亦群飛更亦人尘奏, 今京君心為一人滩褥; 齊心同德創(chuàng)嘉業(yè), 比翼雙飛又是春炫加。
    江涵少年閱讀 102評(píng)論 0 3