iOS中常見的幾種頁面之間的通訊方式_(傳值方式)

1、屬性傳值
前向后傳值。
記?。?br> MainViewController與SecondViewController兩個(gè)視圖控制器,點(diǎn)擊MainViewController中的按鈕將跳轉(zhuǎn)到SecondViewController視圖,同時(shí)想要傳遞一個(gè)值過去。這時(shí)可以利用屬性傳值。
首先SecondViewController視圖中需要有一個(gè)屬性用來存儲(chǔ)傳遞過來的值:
@property(nonatomic,retain) NSString*firstValue ;//屬性傳值
然后MainViewController視圖需要引用SecondViewController視圖的頭文件,在視圖中的按鈕點(diǎn)擊事件中,通過SecondViewController的對(duì)象將需要傳遞的值存在firstValue中:

頁面跳轉(zhuǎn)之后,就能在SecondViewController視圖中,通過存值的屬性,取用剛才傳遞過來的值:
//顯示傳過來的值[_txtFiled setText:_firstValue];//firstValue保存?zhèn)鬟^來的值
2、方法傳值:
需求同一中的 屬性傳值一樣,但是要通過使用方法傳值,可以直接將方法與初始化方法合并,此時(shí)當(dāng)觸發(fā)MainViewController的按鈕點(diǎn)擊事件并跳轉(zhuǎn)到SecondViewController時(shí),在按鈕點(diǎn)擊事件中可以直接通過SecondViewController的初始化,將值保存在firstValue中:
初始化方法如下: 首先SecondViewController視圖中需要有一個(gè)屬性用來存儲(chǔ)傳遞過來的值:
@property(nonatomic,retain) NSString *firstValue;//傳值用
iOS中常見的幾種頁面之間的通訊方式_(傳值方式)

這樣就可以直接通過firstValue屬性獲得傳遞過來的值:
//顯示傳過來的值[_txtFiledsetText:_firstValue];//firstValue保存?zhèn)鬟^來的值3:通知傳值
通知中心NSNotificationCenter提供了一種更加解耦的方式。最典型的應(yīng)用就是任何對(duì)象對(duì)可以發(fā)送通知到中心,同時(shí)任何對(duì)象可以監(jiān)聽中心的通知。發(fā)送通知的代碼如下:
[[NSNotificationCenterdefaultCenter] postNotificationName:@”myNotificationName” object:broadcasterObject];注冊(cè)接收通知的代碼如下:
[[NSNotificationCenterdefaultCenter] addObserver:listenerObject selector:@selector(receivingMethodOnListener:) name:@”myNotificationName” object:nil];注冊(cè)通知的時(shí)候可以指定一個(gè)具體的廣播者對(duì)象,但這不是必須的。你可能注意到了defaultCenter。實(shí)際上這是你在應(yīng)用中會(huì)使用到的唯一的中心。通知會(huì)向整個(gè)應(yīng)用開放,因此只有一個(gè)中心。同時(shí)還有一個(gè)NSDistributedNotificationCenter。這是用來應(yīng)用間通信的。在整個(gè)計(jì)算機(jī)上只有一個(gè)該類型的中心。優(yōu)點(diǎn): 通知的發(fā)送者和接受者都不需要知道對(duì)方??梢灾付ń邮胀ㄖ木唧w方法。通知名可以是任何字符串。缺點(diǎn): 較鍵值觀察需要多點(diǎn)代碼。在刪掉前必須移除監(jiān)聽者。 不能傳大量數(shù)值,只能讓誰去做什么事。
4:代理傳值(Delegate)
其中有兩個(gè)ViewController分別對(duì)應(yīng)兩個(gè)界面,一個(gè)協(xié)議PassValueDelegate用來實(shí)現(xiàn)傳值協(xié)議,UserEntity是傳遞數(shù)據(jù)的對(duì)象。以下是實(shí)現(xiàn)的效果:點(diǎn)擊Open進(jìn)入Second界面,輸入完畢點(diǎn)擊OK后回到First界面并顯示結(jié)果
協(xié)議中聲明的方法:
copy

import

@ class UserEntity;

@protocol PassValueDelegate

-( void)passValue:(UserEntity *)value;

@end
在第一個(gè)窗口實(shí)現(xiàn)協(xié)議:

import

import "PassValueDelegate.h"

//第一個(gè)窗口遵守PassValueDelegate
@interface ViewController : UIViewController

@property (retain, nonatomic) IBOutlet UILabel *nameLabel;
@property (retain, nonatomic) IBOutlet UILabel *ageLabel;
@property (retain, nonatomic) IBOutlet UILabel *gendarLabel;

  • (IBAction)openBtnClicked:(id)sender;

@end

.m文件中實(shí)現(xiàn)協(xié)議的方法:
[cpp]view plaincopy
//實(shí)現(xiàn)協(xié)議,在第一個(gè)窗口顯示在第二個(gè)窗口輸入的值方法
-( void)passValue:(UserEntity *)value
{
self.nameLabel.text = value.userName;
self.ageLabel.text = [NSString stringWithFormat:@"%d",value.age];
self.gendarLabel.text = value.gendar;
}

點(diǎn)擊Open按鈕所觸發(fā)的事件:
[cpp]view plaincopy
//點(diǎn)擊進(jìn)入第二個(gè)窗口的方法

  • (IBAction)openBtnClicked:(id)sender {
    SecondViewController *secondView = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:[NSBundle mainBundle]];
    //設(shè)置第二個(gè)窗口中的delegate為第一個(gè)窗口的self
    secondView.delegate = self;

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

第二個(gè)窗口中聲明一個(gè)NSObject對(duì)象,該對(duì)象遵守PassValueDelegate協(xié)議:
[cpp]view plaincopy

import

import "PassValueDelegate.h"

@interface SecondViewController : UIViewController

@property (retain, nonatomic) IBOutlet UITextField *nameTextField;
@property (retain, nonatomic) IBOutlet UITextField *ageTextFiled;
@property (retain, nonatomic) IBOutlet UITextField *gendarTextField;

//這里用assign而不用retain是為了防止引起循環(huán)引用。
@property(nonatomic,assign) NSObject *delegate;

  • (IBAction)okBtnClicked:(id)sender;
  • (IBAction)closeKeyboard:(id)sender;

@end

輸入完畢后,點(diǎn)擊OK按鈕所觸發(fā)的事件:
[cpp]view plaincopy

  • (IBAction)okBtnClicked:(id)sender {
    UserEntity *userEntity = [[UserEntity alloc] init];
    userEntity.userName = self.nameTextField.text;
    userEntity.gendar = self.gendarTextField.text;
    userEntity.age = [self.ageTextFiled.text intValue];

    //通過委托協(xié)議傳值
    [self.delegate passValue:userEntity];
    //退回到第一個(gè)窗口
    [self.navigationController popViewControllerAnimated:YES];

    [userEntity release];
    }

以上就實(shí)現(xiàn)了使用Delegate在兩個(gè)ViewController之間傳值,這種場(chǎng)景一般應(yīng)用在進(jìn)入子界面輸入信息,完后要把輸入的信息回傳給前一個(gè)界面的情況,比如修改用戶個(gè)人信息,點(diǎn)擊修改進(jìn)入修改界面,修改完后到顯示界面顯示修改后的結(jié)果。
5:Block傳值1.第一頁中 聲明一個(gè) block,需要傳入一個(gè)顏色 , 讓當(dāng)前的view 變色
// 聲明一個(gè) block, 需要傳入一個(gè)顏色 ,讓當(dāng)前的 view 變色
void (^changeColor)( UIColor *color) =^( UIColor *color){
self . view .backgroundColor = color;
};
2 . 第一頁中//block 傳值 ---------將 block 給第二個(gè)頁面
SecondViewController *secondVC = [[SecondViewController alloc ] init ];
//block 傳值 --------- 將 block給第二個(gè)頁面
secondVC. block = changeColor;
3.第二頁中定義 -- 當(dāng) block變量作為一個(gè)類的屬性 , 必須要使用copy 修飾
//block 傳值 --------- 將 block給第二個(gè)頁面
//block 傳值 --- 當(dāng) block變量作為一個(gè)類的屬性 , 必須要使用 copy修飾
@property ( nonatomic , copy) void (^block)( UIColor *color);
4.在第二頁中給block傳值
//block 傳值 --------- 將傳值給 block
NSArray *array = [NSArray arrayWithObjects :[ UIColor yellowColor ], [UIColor cyanColor], [ UIColor greenColor ], [UIColor brownColor], nil];
self . block([array objectAtIndex :rand () % 4]);

還有單例傳值丶數(shù)據(jù)共享等傳值方式,但是平時(shí)工作中很少用到,就不做解釋.最常用的幾種傳值方式也就以上五種,各有優(yōu)缺點(diǎn)吧,希望這篇文章能幫到大家把,感謝各位~

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

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

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