博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS 传值
阅读量:6854 次
发布时间:2019-06-26

本文共 4205 字,大约阅读时间需要 14 分钟。

hot3.png

iOS 传值

一、属性传值(省略)

二、代理传值 RootViewController <- FirstViewController

RootViewController界面代码:

.m文件#import "RootViewController.h"#import "FirstViewController.h"@interface RootViewController ()
//第四步@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 
@protocol FirstViewControllerDelegate 
//第一步//协议方法-(void)sendValue:(NSString *)str;@end@interface FirstViewController : UIViewController@property (nonatomic,assign)id
 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 
typedef void(^SENDBLOCK)(NSString *str);//第一步@interface FirstViewController : UIViewController@property (nonatomic,copy)SENDBLOCK block;//第二步-(void)sendText:(SENDBLOCK)block;//第三步@endFirstViewController.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 
@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];}

转载于:https://my.oschina.net/GeeksChen/blog/637112

你可能感兴趣的文章