UI04-界面間的通信(正向傳值癌椿、反向傳值)

ViewController.m

#import "ViewController.h"
#import "SecondViewController.h"
//遵循協(xié)議
@interface ViewController ()<SecondViewControllerDelegate>
@property(nonatomic,retain)UITextField *textField;
@end
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 200, 100, 50)];
    self.textField.placeholder = @"請(qǐng)輸入內(nèi)容";
    self.textField.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.textField];
    //打開用戶交互  所有view屬性
    self.textField.userInteractionEnabled = YES;
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(100, 50, 100, 100);
    button.backgroundColor = [UIColor orangeColor];
    [button setTitle:@"Hello" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    }
-(void)buttonAction:(UIButton *)button{
    SecondViewController *secondViewController = [[SecondViewController alloc] init];
    //建立代理關(guān)系
    secondViewController.delegate = self;
     secondViewController.text = self.textField.text;
    [self presentViewController:secondViewController animated:YES completion:nil];
   
    }
//點(diǎn)擊回收鍵面

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    [self.view endEditing:YES];
}

-(void)getTextFieldValue:(NSString *)text{
    self.textField.text = text;

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

@end

SecondViewController.h

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor brownColor];
    
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 40)];
    lable.text = self.text;
    lable.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:lable];
    
    
    self.textField  = [[UITextField alloc] initWithFrame:CGRectMake(100, 300, 100, 50)];
    self.textField.placeholder = @"請(qǐng)輸入";
    [self.view addSubview:self.textField];
     UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    button1.frame = CGRectMake(100, 200, 100, 100);
    button1.backgroundColor = [UIColor orangeColor];
    [button1 setTitle:@"NEST" forState:UIControlStateNormal];
    [button1 addTarget:self action:@selector(button1Action:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button1];
    }
-(void)button1Action:(UIButton *)button{
    [self.delegate getTextFieldValue:self.textField.text];
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

SecondViewController.m

#import "SecondViewController.h"

@interface SecondViewController ()

@property(nonatomic,retain)UITextField *textField;

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor brownColor];
    
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 40)];
    lable.text = self.text;
    lable.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:lable];
     self.textField  = [[UITextField alloc] initWithFrame:CGRectMake(100, 300, 100, 50)];
    self.textField.placeholder = @"請(qǐng)輸入";
    [self.view addSubview:self.textField];
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    button1.frame = CGRectMake(100, 200, 100, 100);
    button1.backgroundColor = [UIColor orangeColor];
    [button1 setTitle:@"NEST" forState:UIControlStateNormal];
    [button1 addTarget:self action:@selector(button1Action:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button1];
    
}
-(void)button1Action:(UIButton *)button{
    [self.delegate getTextFieldValue:self.textField.text];
    [self dismissViewControllerAnimated:YES completion:nil];
    
}


屏幕快照 2017-08-15 下午7.20.36.png
屏幕快照 2017-08-15 下午7.20.44.png

界面基本上就是上面的圖窍霞,創(chuàng)建的特別簡(jiǎn)單犁嗅,大家馬馬虎虎看吧 懂思想就好,
在第一個(gè)界面的textfiled上輸入任意值 點(diǎn)擊Hello的button就跳轉(zhuǎn)到第二個(gè)界面缔赠,并且輸入的值傳到了第二界面的lable中衍锚。
同樣在第二個(gè)界面的textfiled中輸入任意值點(diǎn)擊NEST就跳轉(zhuǎn)到第一個(gè)界面,并且傳值到第一個(gè)界面的textFiled中(這個(gè)是因?yàn)槲姨珣辛藳]新建新的lable嗤堰,直接在第一個(gè)界面的textFiled上傳了)构拳。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市梁棠,隨后出現(xiàn)的幾起案子置森,更是在濱河造成了極大的恐慌,老刑警劉巖符糊,帶你破解...
    沈念sama閱讀 221,635評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件凫海,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡男娄,警方通過查閱死者的電腦和手機(jī)行贪,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,543評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來模闲,“玉大人建瘫,你說我怎么就攤上這事∈郏” “怎么了啰脚?”我有些...
    開封第一講書人閱讀 168,083評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)实夹。 經(jīng)常有香客問我橄浓,道長(zhǎng)粒梦,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,640評(píng)論 1 296
  • 正文 為了忘掉前任荸实,我火速辦了婚禮匀们,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘准给。我一直安慰自己泄朴,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,640評(píng)論 6 397
  • 文/花漫 我一把揭開白布露氮。 她就那樣靜靜地躺著祖灰,像睡著了一般。 火紅的嫁衣襯著肌膚如雪沦辙。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,262評(píng)論 1 308
  • 那天讹剔,我揣著相機(jī)與錄音油讯,去河邊找鬼。 笑死延欠,一個(gè)胖子當(dāng)著我的面吹牛陌兑,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播由捎,決...
    沈念sama閱讀 40,833評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼兔综,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了狞玛?” 一聲冷哼從身側(cè)響起软驰,我...
    開封第一講書人閱讀 39,736評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎心肪,沒想到半個(gè)月后锭亏,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,280評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡硬鞍,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,369評(píng)論 3 340
  • 正文 我和宋清朗相戀三年慧瘤,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片固该。...
    茶點(diǎn)故事閱讀 40,503評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡锅减,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出伐坏,到底是詐尸還是另有隱情怔匣,我是刑警寧澤,帶...
    沈念sama閱讀 36,185評(píng)論 5 350
  • 正文 年R本政府宣布桦沉,位于F島的核電站劫狠,受9級(jí)特大地震影響拴疤,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜独泞,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,870評(píng)論 3 333
  • 文/蒙蒙 一呐矾、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧懦砂,春花似錦蜒犯、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,340評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至羽资,卻和暖如春淘菩,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背屠升。 一陣腳步聲響...
    開封第一講書人閱讀 33,460評(píng)論 1 272
  • 我被黑心中介騙來泰國(guó)打工潮改, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人腹暖。 一個(gè)月前我還...
    沈念sama閱讀 48,909評(píng)論 3 376
  • 正文 我出身青樓汇在,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親脏答。 傳聞我的和親對(duì)象是個(gè)殘疾皇子糕殉,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,512評(píng)論 2 359

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