iOS 传值

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

@implementation 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];
}


你可能感兴趣的:(iOS 传值)