iOS 五种传值方式

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS 五种传值方式相关的知识,希望对你有一定的参考价值。

1.属性传值和代理传值

#import <UIKit/UIKit.h>
#import "SecondViewController.h"
@interface ViewController : UIViewController<UITextFieldDelegate,postValuedelegate>

@property (nonatomic ,strong) UITextField *textfld;
@property (nonatomic ,strong) UIButton *button;
@end
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //设置背景色
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"18103807_114944308127_2.jpg"]];
    
    self.textfld = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 100, 50)];
    self.textfld.backgroundColor = [UIColor grayColor];
    self.textfld.textColor = [UIColor blueColor];
    //指定隐藏键盘的代理
    self.textfld.delegate = self;
    [self.view addSubview:self.textfld];
    //设置跳到下一页的按钮
    self.button = [[UIButton alloc] initWithFrame:CGRectMake(120, 160, 80, 40)];
    [self.button setTitle:@"下一页" forState:UIControlStateNormal];
    [self.button addTarget:self action:@selector(nextPage) forControlEvents: UIControlEventTouchUpInside ];
    [self.view addSubview:self.button];
}


//隐藏键盘
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    if ([self.textfld isFirstResponder]) {
        [self.textfld resignFirstResponder];
    }
    
    return YES;
}
-(void)nextPage
{
    
    SecondViewController *second = [[SecondViewController alloc] init];
    //把本页的值赋给下一页的属性,实现传值
    second.str = self.textfld.text;
    [self presentViewController:second animated:YES completion:^{
        NSLog(@"跳转成功");
    }];
    second.delegate = self;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//实现代理方法
-(void)postValue:(NSString *)info{
    self.textfld.text = info;
    
}

@end
//第二页代码
#import <UIKit/UIKit.h>
//申明一个传值协议
@protocol postValuedelegate <NSObject>
//传值协议的方法
-(void)postValue:(NSString *) info;

@end


@interface SecondViewController : UIViewController<UITextFieldDelegate>
@property (nonatomic ,strong) NSString *str;
@property (nonatomic ,strong)  UITextField *textFil;
@property (nonatomic ,assign) id<postValuedelegate> delegate;

@end
#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"5622630220130817130527093.jpg"]];
    self.textFil = [[UITextField alloc ] initWithFrame:CGRectMake(100, 100, 100, 50)];
    self.textFil.backgroundColor = [UIColor grayColor];
    self.textFil.textColor = [UIColor greenColor];
    //属性传值
    self.textFil.text = self.str;
    self.textFil.delegate = self;
    [self.view addSubview:self.textFil];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    if (self.delegate) {
        [self.delegate postValue:self.textFil.text];
    }
    if ([self.textFil isFirstResponder]) {
        [self.textFil resignFirstResponder];
    }
    [self dismissViewControllerAnimated:YES completion:nil];
    
    return YES;
}


@end

2.代码块传值

#import <UIKit/UIKit.h>
#import "SecondViewController.h"
@interface ViewController : UIViewController<UITextFieldDelegate>
@property (nonatomic ,strong) UITextField *textName;
@property (nonatomic ,strong) UIButton *button;

@end
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"5622630220130817130527093.jpg"]];
    self.textName = [[UITextField alloc ] initWithFrame:CGRectMake(100, 100, 100, 50)];
    self.textName.backgroundColor = [UIColor grayColor];
    self.textName.textColor = [UIColor greenColor];
    self.textName.delegate = self;
    [self.view addSubview:self.textName];
    self.button = [[UIButton alloc] initWithFrame:CGRectMake(120, 160, 80, 40)];
    [self.button setTitle:@"下一页" forState:UIControlStateNormal];
    [self.button addTarget:self action:@selector(nextPage) forControlEvents: UIControlEventTouchUpInside ];
    [self.view addSubview:self.button];
    
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    if ([self.textName isFirstResponder]) {
        [self.textName resignFirstResponder];
    }
    return YES;
}



-(void)nextPage
{
    SecondViewController *second =  [[SecondViewController alloc] init];
    second.str = self.textName.text;
    second.postvalueb = ^(NSString *str){
        self.textName.text = str;
        NSLog(@"%@",str);
    };
    [self presentViewController:second animated:YES completion:^{
        NSLog(@"页面跳转成功");
    }];
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
#import <UIKit/UIKit.h>
typedef void(^postValueBlock)(NSString *);

@interface SecondViewController : UIViewController<UITextFieldDelegate>
@property (nonatomic ,strong) NSString *str;
@property (nonatomic ,strong) postValueBlock postvalueb;
@property (nonatomic ,strong)  UITextField *textFil;
@end
#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

- (void)viewDidLoad {
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"5622630220130817130527093.jpg"]];
    self.textFil = [[UITextField alloc ] initWithFrame:CGRectMake(100, 100, 100, 50)];
    self.textFil.backgroundColor = [UIColor grayColor];
    self.textFil.textColor = [UIColor greenColor];
    //属性传值
    self.textFil.text = self.str;
    self.textFil.delegate = self;
    [self.view addSubview:self.textFil];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if(self.postvalueb){
               self.postvalueb(self.textFil.text);
        
    }
    if ([self.textFil isFirstResponder]) {
        [self.textFil resignFirstResponder];
    }
    [self dismissViewControllerAnimated:YES completion:nil];
    
    return YES;
}


@end

 3.单列传值

#import <UIKit/UIKit.h>
//创建一个单列类
@interface MyPickerView : UIPickerView<NSCopying>
+(MyPickerView *)shareInstance;
@end
//创建一个单列类
#import "MyPickerView.h"
static MyPickerView *singleton;
@implementation MyPickerView
+(MyPickerView *)shareInstance
{    if(singleton == nil)
     {
       singleton = [[MyPickerView alloc] init];
     }
    return singleton;
}
+(instancetype)allocWithZone:(struct _NSZone *)zone{
    if (singleton == nil) {
        singleton = [super allocWithZone:zone];
    }
    return singleton;
}
-(id)copyWithZone:(NSZone *)zone
{
    return self;
}

@end
#import <UIKit/UIKit.h>
#import "MyPickerView.h"
#import "SecondViewController.h"
@interface ViewController : UIViewController<UIPickerViewDataSource,UIPickerViewDelegate>
@property (nonatomic ,strong) NSArray *array;
@property (nonatomic ,strong) MyPickerView *mypickerV;
@property (nonatomic ,strong)  UIButton *button;


@end

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor brownColor];
    self.array = @[@"aa",@"bb",@"cc",@"dd"];
    self.mypickerV = [[MyPickerView alloc] initWithFrame:CGRectMake(100, 200, 100, 100)];
    self.mypickerV.delegate = self;
    self.mypickerV.dataSource = self;
    self.mypickerV.backgroundColor = [UIColor grayColor];
    [self.view addSubview:self.mypickerV];
    
    
    self.button = [[UIButton alloc] initWithFrame:CGRectMake(100, 310, 100, 40)];
    self.button.backgroundColor = [UIColor greenColor];
    [self.button setTitle:@"下一页" forState: UIControlStateNormal];
    [self.button addTarget:self action:@selector(nextPage) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.button];
    
    
}


-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    
    return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return self.array.count;
}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    
    return self.array[row];
    
}

-(void)nextPage
{
    SecondViewController *second = [[SecondViewController alloc] init];
    [self presentViewController:second animated:YES completion:nil];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
#import <UIKit/UIKit.h>
#import "MyPickerView.h"
#import "ThirdViewController.h"
@interface SecondViewController : UIViewController
@property (nonatomic ,strong) MyPickerView  *mypick;
@property (nonatomic ,strong)  UIButton *button;
@end

#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blueColor];
        self.mypick = [[MyPickerView alloc] init];
          [self.view addSubview:self.mypick];
    
    self.button = [[UIButton alloc] initWithFrame:CGRectMake(100, 310, 100, 40)];
    self.button.backgroundColor = [UIColor greenColor];
    [self.button setTitle:@"下一页" forState: UIControlStateNormal];
    [self.button addTarget:self action:@selector(nextPage) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.button];
    self. mypick.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"u=113715476,712357466&fm=21&gp=0.jpg"]];
 
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}


-(void)nextPage
{
    ThirdViewController *third = [[ThirdViewController alloc] init];
    
    [self viewDidAppear:YES];
    [self presentViewController:third animated:YES completion:nil];
    
}

@end
#import <UIKit/UIKit.h>
#import "MyPickerView.h"
#import "ViewController.h"
@interface ThirdViewController : UIViewController
@property (nonatomic ,strong) MyPickerView *mypick;
@property (nonatomic ,strong)  UIButton *button;
@end
#import "ThirdViewController.h"

@interface ThirdViewController ()

@end

@implementation ThirdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.mypick = [[MyPickerView alloc] init];
    [self.view addSubview:self.mypick];
    
    self.button = [[UIButton alloc] initWithFrame:CGRectMake(100, 310, 100, 40)];
    self.button.backgroundColor = [UIColor greenColor];
    [self.button setTitle:@"下一页" forState: UIControlStateNormal];
    [self.button addTarget:self action:@selector(nextPage) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.button];


}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)nextPage
{
    ViewController *second = [[ViewController alloc] init];
   
    [self presentViewController:second animated:YES completion:nil];
    
}
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
}

@end

 4.通知传值

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    NSDictionary *dic = @{@"name":@"liumu",
                          @"age":@"22"};
                          
    
    NSNotification *notification = [[NSNotification alloc] initWithName:@"show" object:nil userInfo:dic];
    // 取value值
    [[NSNotificationCenter defaultCenter]
     postNotificationName:@"show" object:notification];
    //发送消息取键值
   // [[NSNotificationCenter defaultCenter] postNotification:notification];
    
      SecondViewController *second = [[SecondViewController alloc] init];
    [self presentViewController:second animated:YES completion:nil];

}
@end
#import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController
@property (nonatomic ,strong) UITextField *TextFil;
@end

#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor purpleColor];
    self.TextFil.backgroundColor = [UIColor blueColor];
    self.TextFil = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    self.TextFil.textColor = [UIColor greenColor];
    [self.view addSubview:self.TextFil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(Show:) name:@"show" object:nil];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}

//传值
-(void)Show:(NSNotification *)notification
{
   NSString *str=(NSString *)[notification object];
    //self.TextFil.text = str;
    NSLog(@"%@",str);
}

//注册完毕,销毁通知
-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"show" object:nil];
}




@end

 

以上是关于iOS 五种传值方式的主要内容,如果未能解决你的问题,请参考以下文章

vue组件之间的五种传值方法(父子兄弟跨组件)

iOS开发之界面间传值

iOS开发之界面间传值

iOS学习之六种传值方式

iOS 页面间几种传值方式(属性,代理,block,单例,通知)

iOS 页面间几种传值方式(属性,代理,block,单例,通知)