最近想整理一下頁(yè)面間傳值的方法匯總,所以寫(xiě)了這個(gè)demo 驼修,在這里與大家分享
在這個(gè)demo中我們需要建兩個(gè)控制器 從第一個(gè)控制器可以模態(tài)到第二個(gè)控制器中殿遂,兩個(gè)控制器中各有一個(gè)TextField, 如果從第一個(gè)頁(yè)面向第二個(gè)頁(yè)面?zhèn)髦悼梢允褂脤傩缘姆椒ㄒ腋鳎菑牡诙€(gè)向第一個(gè)頁(yè)面?zhèn)骶蜁?huì)失效墨礁,可以用以下這四種方式:
單例傳值
單例傳值是利用了單例的生命周期為整個(gè)程序這一特點(diǎn)來(lái)進(jìn)行傳值的
- 新建單例類(lèi)
.h文件內(nèi)
@interface DataManager : NSObject
+(instancetype)Default;
@property(nonatomic,strong)NSString *dataManagerString;
@end
.m文件內(nèi)
#import "DataManager.h"
static DataManager *manager = nil;
@implementation DataManager
+(instancetype)Default
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
manager = [[DataManager alloc]init];
});
return manager;
}
@end
- 第二個(gè)頁(yè)面中賦值
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//給單例賦值
[DataManager Default].dataManagerString = self.firstField.text;
[self dismissViewControllerAnimated:YES completion:nil];
}
- 第一個(gè)頁(yè)面中傳值
-(void)viewWillAppear:(BOOL)animated{
//利用單例傳值
self.field.text = [DataManager Default].dataManagerString;
}
block傳值
block傳值是利用block函數(shù)回調(diào)這一功能來(lái)實(shí)現(xiàn)的
- 第二個(gè)頁(yè)面中新建block屬性
#import <UIKit/UIKit.h>
typedef void(^passValueBlock)(NSString *blockString);
@interface SecondViewController : UIViewController
@property(nonatomic,copy)passValueBlock block;
@end
- 模態(tài)返回第一個(gè)頁(yè)面時(shí)調(diào)用block方法
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//調(diào)用block
self.block(self.secondField.text);
[self dismissViewControllerAnimated:YES completion:nil];
}
- 第一個(gè)頁(yè)面中實(shí)現(xiàn)block方法
//進(jìn)入第二個(gè)頁(yè)面的點(diǎn)擊事件
- (IBAction)blockButton:(id)sender {
SecondViewController *secondVC = [[SecondViewController alloc]init];
//block傳值
secondVC.block = ^(NSString *string){
self.field.text = string;
};
[self presentViewController:secondVC animated:YES completion:nil];
}
代理傳值
- 在第二個(gè)頁(yè)面中聲明協(xié)議和代理屬性 以及 傳值所用的方法
#import <Foundation/Foundation.h>
@protocol passValueDelegate <NSObject>
-(void)passString:(NSString *)string;
@end
#import <UIKit/UIKit.h>
@interface ThirdViewController : UIViewController
@property(nonatomic,weak)id<passValueDelegate>delegate;
@end
- 第二個(gè)頁(yè)面中調(diào)用方法
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//代理傳值
[self.delegate passString:self.thirdField.text];
[self dismissViewControllerAnimated:YES completion:nil];
}
- 第一個(gè)頁(yè)面中實(shí)現(xiàn)方法
//點(diǎn)擊進(jìn)入第二個(gè)界面的方法
- (IBAction)delegateButton:(id)sender {
ThirdViewController *thirdVC = [[ThirdViewController alloc]init];
thirdVC.delegate = self;
[self presentViewController:thirdVC animated:YES completion:nil];
}
//代理方法的實(shí)現(xiàn)
-(void)passString:(NSString *)string{
self.field.text = string;
}
通知中心傳值
- 在第二個(gè)頁(yè)面中新建通知中心并賦值
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//通知中心傳值
NSString *string = self.fourthField.text;
[[NSNotificationCenter defaultCenter] postNotificationName:@"passValue" object:string];
[self dismissViewControllerAnimated:YES completion:nil];
}
- 在第一個(gè)頁(yè)面中設(shè)置通知中心
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor lightGrayColor];
self.field = [[UITextField alloc]initWithFrame:CGRectMake(100, 200, 200, 50)];
self.field.backgroundColor = [UIColor cyanColor];
[self.view addSubview:self.field];
//設(shè)置通知中心
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(passAction:) name:@"passValue" object:nil];
}
- 通知中心執(zhí)行的方法實(shí)現(xiàn)
//通知中心執(zhí)行的方法
- (void)passAction:(NSNotification *)noti{
self.field.text = noti.object;
}
- 最后一定要在dealloc方法中撤銷(xiāo)通知中心
//移除通知中心
-(void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
需要的朋友請(qǐng)點(diǎn)擊這里下載源代碼參考