iOS中几种传值的方式

1.ios里面传值的方式很多中现在介绍几种常用的传值方式:

第一种: 属性传值方式:

 首先要建两个controller 分别为 RootviewController和 DetailviewController 

在Detailviewcontrooler.h实现的代码:(暴露出属性)

 #import <UIKit/UIKit.h>

 @interface DetailViewController : UIViewController

@property (nonatomic,copy)NSString *textString;//属性传值

@end

在Detailviewcontroller.m里面的实现代码:

@interface DetailViewController ()

 @end

@implementation DetailViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    //添加一个返回按钮

    UIButton *backBtn = [[UIButton alloc]initWithFrame:CGRectMake(15,30 , 60, 30)];

    [backBtn addTarget:self action:@selector(BACK) forControlEvents:UIControlEventTouchUpInside];

    [backBtn setTitle:@"返回" forState:UIControlStateNormal];

    [self.view addSubview:backBtn];

    //创建一个label,来接收传过来的数值

    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(100, 200, 100, 30)];

    label.textColor = [UIColor redColor];

    label.backgroundColor = [UIColor grayColor];

    label.textAlignment = NSTextAlignmentCenter;//对齐的方式

    label.numberOfLines = 0; //不分行数

    label.text = self.textString;//传值过来

    [self.view addSubview:label]; 

}

-(void)BACK {

     [self dismissViewControllerAnimated:NO completion:nil];

 }

在Rootviewcontroller.m 的实现代码:

#import "RootViewController.h"

#import "DetailViewController.h"

@interface RootViewController ()

@property (nonatomic,retain)UITextField *textfield;

@end

@implementation RootViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    self.textfield = [[UITextField alloc]initWithFrame:CGRectMake(100, 30, 100, 30)];

    self.textfield.backgroundColor = [UIColor grayColor];

    self.textfield.layer.cornerRadius = 10.0;

    [self.view addSubview:self.textfield];

    //创建一个button传递事件

    UIButton *custombutton = [[UIButton alloc]init];

    custombutton.frame = CGRectMake(110, 100,80, 40);

    [custombutton setTitle:@"入栈显示" forState:UIControlStateNormal ];

    [custombutton addTarget:self action:@selector(handTap:) forControlEvents:UIControlEventTouchUpInside];

    custombutton.backgroundColor = [UIColor redColor];

    custombutton.layer.cornerRadius = 10.0;

    [self.view addSubview:custombutton];  

}

-(void)handTap:(UIButton *)sender{

    DetailViewController *detailVC = [[DetailViewController alloc]init];

    detailVC.textString = self.textfield.text;//后面的赋值给前面的

    [self presentViewController:detailVC animated:NO completion:nil];

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

  }

//点击空白处隐藏键盘

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

    [super touchesBegan:touches withEvent:event];

    [self.view endEditing:YES]; //结束编辑

}

最后自己测试一下就可以成功了!!

第二种:通知(NOtification)传值: 直接上代码:<后面往前面传值的方法>

在.RootviewController.m的代码:

//     是后面往前面传值 b == >   a

 #import "RootViewController3.h"

#import "DetailViewController3.h"

@interface RootViewController3 ()

@property(nonatomic,strong)UILabel *label;

@end

 @implementation RootViewController3

 - (void)viewDidLoad {

    [super viewDidLoad];

     //注册一个通知

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(callBack:) name:@"test" object:nil];

    

    //创建一个button传递事件

    UIButton *custombutton = [[UIButton alloc]init];

    custombutton.frame = CGRectMake(110, 100,80, 40);

    [custombutton setTitle:@"rootVC" forState:UIControlStateNormal ];

    [custombutton addTarget:self action:@selector(method) forControlEvents:UIControlEventTouchUpInside];

    custombutton.backgroundColor = [UIColor redColor];

    custombutton.layer.cornerRadius = 10.0;

    [self.view addSubview:custombutton];

    //创建一个label,来接收传过来的数值

    self.label = [[UILabel alloc]initWithFrame:CGRectMake(100, 200, 100, 30)];

    self.label.textColor = [UIColor redColor];

    self.label.backgroundColor = [UIColor grayColor];

    self.label.textAlignment = NSTextAlignmentCenter;//对齐的方式

    self.label.numberOfLines = 0; //不分行数

    [self.view addSubview:self.label];

 }

-(void)method{

     DetailViewController3 *detailVC = [[DetailViewController3 alloc]init];

    [self presentViewController:detailVC animated:YES completion:nil];

 }

 -(void)callBack:(NSNotification  *)noti{

    NSLog(@"call bcak  is notification");

    self.label.text = noti.object;

    [self dismissViewControllerAnimated:YES completion:nil];

 }

 在DetailViewController.m 的代码:

#import "DetailViewController3.h"

@interface DetailViewController3 ()

@property (nonatomic,strong) UITextField *textfield;

@end

@implementation DetailViewController3

 - (void)viewDidLoad {

    [super viewDidLoad];

    //创建一个button传递事件

    UIButton *custombutton = [[UIButton alloc]init];

    custombutton.frame = CGRectMake(110, 100,80, 40);

    [custombutton setTitle:@"DetailVC" forState:UIControlStateNormal ];

    [custombutton addTarget:self action:@selector(handTap:) forControlEvents:UIControlEventTouchUpInside];

    custombutton.backgroundColor = [UIColor redColor];

    custombutton.layer.cornerRadius = 10.0;

    [self.view addSubview:custombutton];

   //创建一个label,来接收传过来的数值

    self.textfield = [[UITextField alloc]initWithFrame:CGRectMake(100, 30, 100, 30)];

    self.textfield.backgroundColor = [UIColor grayColor];

    self.textfield.layer.cornerRadius = 10.0;

    [self.view addSubview:self.textfield];

 }

-(void)handTap:(UIButton *)sender

{

   //发送一个通知

   [[NSNotificationCenter defaultCenter]postNotificationName:@"test" object:self.textfield.text];

}

 - (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end

测试一下就可以成功了!!

第三种:单例模式传值

首先创建一个单列对象:sington类

在sington.h的代码:

#import <Foundation/Foundation.h>

@interface Singtoln : NSObject

@property (nonatomic,strong)NSString *singletonValue;

+(instancetype)shareSingleton ;

 @end

在sington.m的代码:

#import "Singtoln.h"

static Singtoln *singtolnManager = nil;

@implementation Singtoln

+(instancetype)shareSingleton{

     @synchronized(self){

      if (singtolnManager == nil) {

            singtolnManager = [[Singtoln alloc]init];

     }

 }

  return singtolnManager;

}

@end

在RootviewController.m的代码:

#import "RootViewController4.h"

#import "DetailViewController4.h"

#import "Singtoln.h"

@interface RootViewController4 ()

@property (nonatomic,strong)UITextField *textField;

@end

 @implementation RootViewController4

 - (void)viewDidLoad {

    [super viewDidLoad];

     //创建一个button传递事件

    UIButton *custombutton = [[UIButton alloc]init];

    custombutton.frame = CGRectMake(110, 100,80, 40);

    [custombutton setTitle:@"单例传值" forState:UIControlStateNormal ];

    [custombutton addTarget:self action:@selector(method) forControlEvents:UIControlEventTouchUpInside];

    custombutton.backgroundColor = [UIColor redColor];

    custombutton.layer.cornerRadius = 10.0;

    [self.view addSubview:custombutton];

    

    self.textField = [[UITextField alloc]init];

    self.textField.frame = CGRectMake(100, 150,100,40);

    self.textField.backgroundColor = [UIColor grayColor];

    self.textField.layer.cornerRadius = 5;

    [self.view addSubview:self.textField];

}

-(void)method{

     Singtoln *singtoln = [Singtoln shareSingleton];

    singtoln.singletonValue  = self.textField.text;

     DetailViewController4 *detail4VC = [[DetailViewController4 alloc]init];

    [self presentViewController: detail4VC animated:YES completion:nil];  

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

在DetailViewController.m的代码:

#import "DetailViewController4.h"

#import "Singtoln.h"

@interface DetailViewController4 ()

@property (nonatomic,strong)UITextField *textfield;

@end

 @implementation DetailViewController4

 - (void)viewDidLoad {

    [super viewDidLoad];

  

    self.textfield = [[UITextField alloc]init];

    self.textfield.backgroundColor = [UIColor grayColor];

    self.textfield.frame = CGRectMake(100, 60, 100, 40);

    self.textfield.textAlignment = NSTextAlignmentCenter;

    self.textfield.layer.cornerRadius = 10;

    [self.view addSubview:self.textfield];

    

    self.textfield.text =  [Singtoln shareSingleton].singletonValue;

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end

第四种:代理传值:(后面往前面传值) 

在rootviewcontroller.h的代码:

#import "RootViewController2.h"

#import "DetailViewController2.h"

@interface RootViewController2 ()<PassingDelegate>

@property (nonatomic,strong)UILabel *label;

@end

 @implementation RootViewController2

 - (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    UIButton *customBtn = [[UIButton alloc]init];

    customBtn.frame = CGRectMake(130, 50,80, 40);

    customBtn.backgroundColor = [UIColor redColor];

    [customBtn setTitle:@"代理传值" forState:UIControlStateNormal];

    customBtn.tintColor = [UIColor blackColor];

    [customBtn addTarget:self action:@selector(handTap:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:customBtn];

    

    //创建一个label接受的传过来的值

     self.label = [[UILabel alloc]init];

    self.label.frame = CGRectMake(100, 100, 150, 30);

    self.label.layer.cornerRadius = 10;

    self.label.backgroundColor = [UIColor grayColor];

    self.label.tintColor = [UIColor redColor];

    [self.view addSubview:self.label];

    

}

 -(void)handTap:(UIButton *)sender{

    DetailViewController2 *detail2VC = [[DetailViewController2 alloc]init];

    detail2VC.delegate = self;

    [self presentViewController:detail2VC animated:NO completion:nil];

}

 -(void)changeTitle:(NSString *)str{

    self.label.text = str;

    NSLog(@"%@",str);

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 在DetailviewController.m的代码:

#import "RootViewController2.h"

#import "DetailViewController2.h"

@interface RootViewController2 ()<PassingDelegate>

@property (nonatomic,strong)UILabel *label;

@end

@implementation RootViewController2

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    UIButton *customBtn = [[UIButton alloc]init];

    customBtn.frame = CGRectMake(130, 50,80, 40);

    customBtn.backgroundColor = [UIColor redColor];

    [customBtn setTitle:@"代理传值" forState:UIControlStateNormal];

    customBtn.tintColor = [UIColor blackColor];

    [customBtn addTarget:self action:@selector(handTap:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:customBtn];

    

    //创建一个label接受的传过来的值

     self.label = [[UILabel alloc]init];

    self.label.frame = CGRectMake(100, 100, 150, 30);

    self.label.layer.cornerRadius = 10;

    self.label.backgroundColor = [UIColor grayColor];

    self.label.tintColor = [UIColor redColor];

    [self.view addSubview:self.label];

    

}

-(void)handTap:(UIButton *)sender{

    DetailViewController2 *detail2VC = [[DetailViewController2 alloc]init];

    detail2VC.delegate = self;

    [self presentViewController:detail2VC animated:NO completion:nil];

}

-(void)changeTitle:(NSString *)str{

  

    self.label.text = str;

    NSLog(@"%@",str);

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

原文地址:https://www.cnblogs.com/zhufeng1994/p/4624443.html