您的位置:首页 > 移动开发 > IOS开发

iOS 传值

2016-03-14 00:00 295 查看
摘要: 到目前为止,我做过5种界面之间的传值方式,分别是:属性传值,代理传值,Block传值,单例传值和通知传值。每种传值方式都有自己的操作流程和适用场景。接下来一起练习一下吧。

iOS 传值

一、属性传值(省略)

二、代理传值 RootViewController <- FirstViewController

RootViewController界面代码:

.m文件
#import "RootViewController.h"
#import "FirstViewController.h"
@interface RootViewController ()<FirstViewControllerDelegate>//第四步
@end

-(void)buttonAction:(UIButton *)sender{
FirstViewController *fvc = [[FirstViewController alloc]init];
fvc.delegate = self;//第五步
fvc.text = self.textField.text;
[self.navigationController pushViewController:fvc animated:YES];
}
-(void)sendValue:(NSString *)str{//第六步
self.textField.text = str;
}


FirstViewController界面代码:

.h文件
#import <UIKit/UIKit.h>
@protocol FirstViewControllerDelegate <NSObject>//第一步
//协议方法
-(void)sendValue:(NSString *)str;
@end

@interface FirstViewController : UIViewController
@property (nonatomic,assign)id<FirstViewControllerDelegate> delegate;//第二步
@end

.m文件
-(void)buttonAction:(UIButton *)sender{//第三步
if ([self.delegate respondsToSelector:@selector(sendValue:)]) {
[self.delegate sendValue:self.textField.text];
}
[self.navigationController popToRootViewControllerAnimated:YES];
}


三、Block传值 RootViewController <- FirstViewController

RootViewController界面代码:

-(void)buttonAction:(UIButton *)sender{
FirstViewController *fvc = [[FirstViewController alloc]init];
[fvc sendText:^(NSString *str) {//第六步
self.textField.text = str;
}];
[self.navigationController pushViewController:fvc animated:YES];
}


FirstViewController界面代码:

FirstViewController.h
#import <UIKit/UIKit.h>

typedef void(^SENDBLOCK)(NSString *str);//第一步

@interface FirstViewController : UIViewController

@property (nonatomic,copy)SENDBLOCK block;//第二步
-(void)sendText:(SENDBLOCK)block;//第三步
@end

FirstViewController.m
-(void)buttonAction:(UIButton *)sender{
if (self.block != nil) {//第四步
self.block(self.textField.text);
}
[self.navigationController popToRootViewControllerAnimated:YES];
}
-(void)sendText:(SENDBLOCK)block{//第五步
self.block = block;
}


四、单例传值 双向的

RootViewController界面代码:

.m
#import "RootViewController.h"
#import "FirstViewController.h"
#import "DataManager.h"

@interface RootViewController ()

@end

@im
3ff0
plementation RootViewController

-(void)viewWillAppear:(BOOL)animated{
self.textField.text = [DataManager shareDataManager].text;
}
-(void)buttonAction:(UIButton *)sender{
[DataManager shareDataManager].text = self.textField.text;

FirstViewController *fvc = [[FirstViewController alloc]init];
[self.navigationController pushViewController:fvc animated:YES];
}


FirstViewController界面代码:

#import "FirstViewController.h"
#import "DataManager.h"

@interface FirstViewController ()

@end

@implementation FirstViewController
-(void)viewWillAppear:(BOOL)animated{
self.textField.text = [DataManager shareDataManager].text;
}
-(void)buttonAction:(UIButton *)sender{
[DataManager shareDataManager].text = self.textField.text;

[self.navigationController popToRootViewControllerAnimated:YES];
}


DataManager 单例类代码:

.h文件
#import <Foundation/Foundation.h>
@interface DataManager : NSObject
@property (nonatomic,strong)NSString *text;

+(instancetype)shareDataManager;
@end
.m文件
#import "DataManager.h"

static DataManager *_dataManager = nil;

@implementation DataManager

+(instancetype)shareDataManager{
return [[self alloc]init];
}
+(instancetype)allocWithZone:(struct _NSZone *)zone{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
if (_dataManager == nil) {
_dataManager = [super allocWithZone:zone];
}
});
return _dataManager;
}
@end


五、通知传值

RootViewController 界面代码:

.m 文件
-(void)dealloc{
//移除通知
[[UIApplication sharedApplication] cancelAllLocalNotifications];

}
- (void)viewDidLoad {
//注册通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(postNotification:) name:@"Notification" object:nil];
}
//实现通知方法
-(void)postNotification:(NSNotification *)sender{
self.view.backgroundColor = [UIColor grayColor];
}


FirstViewCOntroller 界面代码:

.m 文件
#import "FirstViewController.h"

@interface FirstViewController ()

@end
@implementation FirstViewController

-(void)buttonAction:(UIButton *)sender{
//发送通知
[[NSNotificationCenter defaultCenter] postNotificationName:@"Notification" object:nil];

[self.navigationController popToRootViewControllerAnimated:YES];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  传值