iOS開發(fā)———界面間傳值

在開發(fā)中最最常用的無非就是界面間的互相傳值了,今天為大家介紹最常用的三種傳值方法: 協(xié)議 、 Block、 通知中心 !??!

首先創(chuàng)建第二頁與第三頁以及基礎(chǔ)的button,label,textField等控件,這里不做過多介紹

![ ![2387D0B9-3EB8-4CFF-B47E-ABAEC9CA8205.png](http://upload-images.jianshu.io/upload_images/1714255-3bf4ddb0307b6c31.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)](http://upload-images.jianshu.io/upload_images/1714255-d34eacfef683c412.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

1.首先我們來看協(xié)議傳值

(在第二頁textField中輸入文字,點擊上一頁按鈕,將輸入的文字傳到第一頁的textField上)
具體步驟:
1.在第二頁的.h文件中聲明協(xié)議方法

@protocol passValueDelegate <NSObject>

- (void)passContent:(NSString *)content;

@end

2.在第二頁的.h文件中聲明代理人屬性

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

3.在第二頁.m文件的返回上一頁按鈕的點擊方法里命令代理人執(zhí)行協(xié)議方法
- (void)didClickedBackButton:(UIButton *)button
{

[self.delegate passContent:self.textField.text];

[self.navigationController popToRootViewControllerAnimated:YES];
}

4.在第一頁的.m文件中簽協(xié)議

@interface ViewController ()<passValueDelegate>

5.在第一頁.m文件進(jìn)入下一頁的按鈕點擊方法里設(shè)置代理人

- (void)didClickedButton:(UIButton *)button
{
SecondViewController *svc = [SecondViewController new];

svc.delegate = self;


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

6.在第一頁.m文件中執(zhí)行協(xié)議方法
- (void)passContent:(NSString *)content
{
self.textField.text = content;
}

685DCB19-B86A-4ACD-A687-F397AF9C50AC.png

點擊上一頁按鈕回到第一頁時,相應(yīng)的文字就傳過來了

7D9070C1-EB38-422A-8B94-DED77F9799F1.png

Block傳值

具體步驟 (熟練應(yīng)用后1、2兩步可以寫在一起)
1.在第三頁的.h文件中定義一個有參數(shù)無返回值的block

typedef void (^MyBox)(NSString *str);

2.在第三頁的.h文件中聲明一個block屬性

@property(nonatomic, copy)MyBox box;

3.在第三頁.m文件返回上一頁的按鈕點擊方法中將要傳的值進(jìn)行裝箱操作

- (void)didClickedButton:(UIButton *)button
{

self.box(self.textField.text);

[self.navigationController popViewControllerAnimated:YES];
}

4.在前一頁的.m文件跳轉(zhuǎn)到第三頁的按鈕點擊方法中給block一個實現(xiàn)體

- (void)didClickedButton:(UIButton *)button
{
ThreeViewController *tvc = [ThreeViewController new];

tvc.box = ^(NSString *str){
   
    self.textField.text = str;
};

[self.navigationController pushViewController:tvc animated:YES];
}
F77AD8AE-EE05-4DD8-B2D6-B85CD7722951.png

點擊按鈕返回到第二頁的時候


4E9A04EB-3F17-4112-BBE0-47E70CD20ACD.png

3.通知中心

新建工程,創(chuàng)建兩個vc和相應(yīng)的label


5F56D1B2-2489-4330-9CEA-4719495AE563.png

14E5E7CC-6027-440E-AD1F-7D937385BB71.png

1.在第二頁的.m文件中創(chuàng)建通知中心

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

//創(chuàng)建通知中心:
//通知中心發(fā)通知:
//參數(shù)1:通知中心的名字
//參數(shù)2:需要發(fā)送的內(nèi)容,接收者會得到這個對象

[[NSNotificationCenter defaultCenter] postNotificationName:@"整" object:self.label.text];
[self dismissViewControllerAnimated:YES completion:^{
    nil;
}];

}

2.在第一頁.m文件中為當(dāng)前視圖添加通知中心

 [[NSNotificationCenter defaultCenter] addObserver:self 
 selector:@selector(labelNotificationAction:) name:@"整" object:nil];

3.通知中心觸發(fā)的方法

- (void)labelNotificationAction:(NSNotification *)labelNotification
{
self.label.text = labelNotification.object;
}

頁面用模態(tài)方法跳轉(zhuǎn),也可在按鈕點擊方法里push
模態(tài)://模態(tài)過去時候的方法
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
SecondViewController *svc = [SecondViewController new];
[self presentViewController:svc animated:YES completion:^{
nil;
}];

}

在第二頁點擊屏幕時,模態(tài)到第一頁并把label的文字傳給第一頁的label


91C1A189-E619-422C-8000-4077E19095EB.png

這三種方法在實際開發(fā)中很常用,希望大家熟練運用?。。?/h3>

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容