1、屬性傳值
前向后傳值。
記住:
MainViewController與SecondViewController兩個視圖控制器,點擊MainViewController中的按鈕將跳轉(zhuǎn)到SecondViewController視圖,同時想要傳遞一個值過去。這時可以利用屬性傳值。
首先SecondViewController視圖中需要有一個屬性用來存儲傳遞過來的值:
@property(nonatomic,retain) NSString*firstValue ;//屬性傳值
然后MainViewController視圖需要引用SecondViewController視圖的頭文件,在視圖中的按鈕點擊事件中,通過SecondViewController的對象將需要傳遞的值存在firstValue中:
//顯示傳過來的值[_txtFiled setText:_firstValue];//firstValue保存?zhèn)鬟^來的值
2、方法傳值:
需求同一中的 屬性傳值一樣,但是要通過使用方法傳值,可以直接將方法與初始化方法合并,此時當(dāng)觸發(fā)MainViewController的按鈕點擊事件并跳轉(zhuǎn)到SecondViewController時,在按鈕點擊事件中可以直接通過SecondViewController的初始化,將值保存在firstValue中:
初始化方法如下: 首先SecondViewController視圖中需要有一個屬性用來存儲傳遞過來的值:
@property(nonatomic,retain) NSString *firstValue;//傳值用
這樣就可以直接通過firstValue屬性獲得傳遞過來的值:
//顯示傳過來的值[_txtFiledsetText:_firstValue];//firstValue保存?zhèn)鬟^來的值3:通知傳值
通知中心NSNotificationCenter提供了一種更加解耦的方式。最典型的應(yīng)用就是任何對象對可以發(fā)送通知到中心,同時任何對象可以監(jiān)聽中心的通知。發(fā)送通知的代碼如下:
[[NSNotificationCenterdefaultCenter] postNotificationName:@”myNotificationName” object:broadcasterObject];注冊接收通知的代碼如下:
[[NSNotificationCenterdefaultCenter] addObserver:listenerObject selector:@selector(receivingMethodOnListener:) name:@”myNotificationName” object:nil];注冊通知的時候可以指定一個具體的廣播者對象,但這不是必須的。你可能注意到了defaultCenter。實際上這是你在應(yīng)用中會使用到的唯一的中心。通知會向整個應(yīng)用開放,因此只有一個中心。同時還有一個NSDistributedNotificationCenter。這是用來應(yīng)用間通信的。在整個計算機(jī)上只有一個該類型的中心。優(yōu)點: 通知的發(fā)送者和接受者都不需要知道對方??梢灾付ń邮胀ㄖ木唧w方法。通知名可以是任何字符串。缺點: 較鍵值觀察需要多點代碼。在刪掉前必須移除監(jiān)聽者。 不能傳大量數(shù)值,只能讓誰去做什么事。
4:代理傳值(Delegate)
其中有兩個ViewController分別對應(yīng)兩個界面,一個協(xié)議PassValueDelegate用來實現(xiàn)傳值協(xié)議,UserEntity是傳遞數(shù)據(jù)的對象。以下是實現(xiàn)的效果:點擊Open進(jìn)入Second界面,輸入完畢點擊OK后回到First界面并顯示結(jié)果
協(xié)議中聲明的方法:
copy
import
@ class UserEntity;
@protocol PassValueDelegate
-( void)passValue:(UserEntity *)value;
@end
在第一個窗口實現(xiàn)協(xié)議:
import
import "PassValueDelegate.h"
//第一個窗口遵守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文件中實現(xiàn)協(xié)議的方法:
[cpp]view plaincopy
//實現(xiàn)協(xié)議,在第一個窗口顯示在第二個窗口輸入的值方法
-( void)passValue:(UserEntity *)value
{
self.nameLabel.text = value.userName;
self.ageLabel.text = [NSString stringWithFormat:@"%d",value.age];
self.gendarLabel.text = value.gendar;
}
點擊Open按鈕所觸發(fā)的事件:
[cpp]view plaincopy
//點擊進(jìn)入第二個窗口的方法
-
(IBAction)openBtnClicked:(id)sender {
SecondViewController *secondView = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:[NSBundle mainBundle]];
//設(shè)置第二個窗口中的delegate為第一個窗口的self
secondView.delegate = self;[self.navigationController pushViewController:secondView animated:YES];
}
第二個窗口中聲明一個NSObject對象,該對象遵守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
輸入完畢后,點擊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];
//退回到第一個窗口
[self.navigationController popViewControllerAnimated:YES];[userEntity release];
}
以上就實現(xiàn)了使用Delegate在兩個ViewController之間傳值,這種場景一般應(yīng)用在進(jìn)入子界面輸入信息,完后要把輸入的信息回傳給前一個界面的情況,比如修改用戶個人信息,點擊修改進(jìn)入修改界面,修改完后到顯示界面顯示修改后的結(jié)果。
5:Block傳值1.第一頁中 聲明一個 block,需要傳入一個顏色 , 讓當(dāng)前的view 變色
// 聲明一個 block, 需要傳入一個顏色 ,讓當(dāng)前的 view 變色
void (^changeColor)( UIColor *color) =^( UIColor *color){
self . view .backgroundColor = color;
};
2 . 第一頁中//block 傳值 ---------將 block 給第二個頁面
SecondViewController *secondVC = [[SecondViewController alloc ] init ];
//block 傳值 --------- 將 block給第二個頁面
secondVC. block = changeColor;
3.第二頁中定義 -- 當(dāng) block變量作為一個類的屬性 , 必須要使用copy 修飾
//block 傳值 --------- 將 block給第二個頁面
//block 傳值 --- 當(dāng) block變量作為一個類的屬性 , 必須要使用 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ù)共享等傳值方式,但是平時工作中很少用到,就不做解釋.最常用的幾種傳值方式也就以上五種,各有優(yōu)缺點吧,希望這篇文章能幫到大家把,感謝各位~