頁面間傳值有兩種:
- 正向傳值(利用屬性傳值就可以了,很簡單)
- 逆向傳值(有3種常用的方法)
- 代理傳值
- block傳值
- 通知中心傳值
我這篇文章分享的是逆向傳值的3種方式(以下內容請配合我寫的簡單的Demo看看點擊下載)
1.代理傳值(相對block,代理更適用于需要實現多個方法)
- 代理傳值第一步: NextViewcontroller中聲明協議(也可以創建一個協議類)
#import <UIKit/UIKit.h>
@protocol ChangeName //協議
-(void)changeName:(NSString*)string;
@end
@interface NextViewController : UIViewController
- 第二步: 聲明代理
ARC(內存自動釋放機制), 使用weak修飾; MRC環境下使用assign修飾
@interface NextViewController : UIViewController
@property (nonatomic,weak)id<ChangeName>delegate; //代理
- 第三步: 在按鈕的觸發方法中調用代理方法
//代理
-(IBAction)delegateAction:(UIButton *)sender {
if (![self.nameTextField.text isEqualToString:@""]) {
[self.delegate changeName:self.nameTextField.text];//寫在前面或寫在dismiss的Block中都可以
}
[self dismissViewControllerAnimated:YES completion:^{
}];
}
- 第四步:在ViewController中導入NextViewController頭文件,并遵守協議
#import "NextViewController.h"http://因為我把協議寫在了NextViewController中所以只要倒入這個頭文件就行了
@interface ViewController ()<ChangeName>
- 第五步:設置代理(因為我用的是storyboard所以在這個跳轉的方法中設置代理)
我以后會寫一篇關于storyboard用法的文章來說說注意事項
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
//讓NextViewController的實例是segue的目標控制器
NextViewController* vc=segue.destinationViewController;//segue是storyboard中的拖線
vc.delegate=self;
}
- 第六步:在ViewController的.m文件中實現協議方法
//協議要實現的方法
-(void)changeName:(NSString *)string
{
self.NameLabel.text=string;
}
2.block傳值
block傳值有4種寫法,在這里我提供一個簡單的寫法,可以在我的鏈接中下載簡單的Demo看看,里面有2種方法
- 第一步:在NextViewController中(.h中 以保證隨時可以訪問)聲明一個Block屬性
用copy修飾
@property (nonatomic,copy) void (^change)(NSString* stringName);//block 寫法1
- 第二步:在NextViewController.m文件中的按鈕觸發方法中調用
-(IBAction)blockAction:(UIButton *)sender {
//block 方法1
self.change(self.nameTextField.text); //寫在前面或寫在dismiss的Block中都可以
[self dismissViewControllerAnimated:YES completion:^{
}];
}
- 第三步:在ViewController.m中導入頭文件并實現block
#import "NextViewController.h"
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
//讓NextViewController的實例是segue的目標控制器
NextViewController* vc=segue.destinationViewController;//segue是storyboard中的拖線
//block 寫法1
__weak __typeof(self) weakSelf = self; //防止循環引用
vc.change=^(NSString* stringName){
weakSelf.NameLabel.text=stringName;
};
}
3.通知中心傳值
這個傳值方式我比較喜歡,因為它寫起來比較簡單,用起來比較方便通知中心NSNotificationCenter是面向程序全局發送消息,在哪都可以接到
,但請注意消息的命名(不要重名)
- 第一步:在ViewController中添加觀察者
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
//通知中心添加觀察者
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeLabelText:) name:@"改變名稱" object:nil];
}
- 第二步:在ViewController.m中實現觀察者接到消息之后執行的方法
-(void)changeLabelText:(NSNotification*)sender
{
self.NameLabel.text=sender.userInfo[@"名字"];
}
- 第三步:在ViewController.m中要移除觀察者
//移除通知中心
-(void)dealloc
{
[[NSNotificationCenter defaultCenter]removeObserver:self];
}
- 第四步:在NextViewController.m中發送消息
//通知中心發送通知
-(IBAction)NSNotificationAction:(UIButton *)sender {
NSDictionary* dic=@{@"名字":self.nameTextField.text};
[[NSNotificationCenter defaultCenter] postNotificationName:@"改變名稱" object:nil userInfo:dic];
[self dismissViewControllerAnimated:YES completion:^{
}];
}
注:相關內容我會繼續更新。如果想找一些iOS方面的代碼可以關注我的簡書,我會持續更新,大家一起探討探討
在此謝謝大家閱讀??