Objective-C的传值
一、属性传值
顾名思义,属性传值就是通过类的属性传值,也是objective–c中最容易理解的传值方法。
代码演示:
这里我们创建两个视图控制器分别是ViewController和SecondeViewController
先在SecondeViewController的声明文件中定义一个属性用于属性传值
SecondeViewControlle.h:
#import <UIKit/UIKit.h>
//注意:这里是SecondeViewController的声明文件中
@interfaceSecondViewController :UIViewController
//定义一个属性
@property(nonatomic,copy)NSString*textString;
@end
SecondeViewControlle.m:
#import"SecondViewController.h"
@interfaceSecondViewController()
@end
@implementationSecondViewController
- (void)viewDidLoad {
[superviewDidLoad];
self.view.backgroundColor= [UIColorwhiteColor];
//初始标签
UILabel*aLabel = [[UILabelalloc]initWithFrame:CGRectMake(100,200,214,50)];
//这里将属性传递的值赋给标签显示
aLabel.text=_textString;
[self.viewaddSubview:aLabel];
//初始化按钮
UIButton*aButton= [UIButtonbuttonWithType:UIButtonTypeSystem];
aButton .frame=CGRectMake(100,280,214,50);
[aButtonsetTitle:@"上一页"forState:UIControlStateNormal];
[aButtonaddTarget:selfaction:@selector(buttonTaped:)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview: aButton ];
}
//响应上一页按钮的点击事件返回上一页
- (void)buttonTaped:(UIButton*) button{
[selfdismissViewControllerAnimated:YEScompletion:nil];
}
viewController.m:
#import"ViewController.h"
#import"SecondViewController.h"
@interfaceViewController()
@property(nonatomic,strong)UITextField*aTextField;
@end
@implementationViewController
- (void)viewDidLoad {
[superviewDidLoad];
//初始化文本输入框
_aTextField= [[UITextFieldalloc]initWithFrame:CGRectMake(100,200,214,50)];
_aTextField.borderStyle=UITextBorderStyleLine;
_aTextField.layer.borderWidth=2;
[self.viewaddSubview:_aTextField];
//初始化按钮
UIButton*aButton = [UIButtonbuttonWithType:UIButtonTypeSystem];
aButton.frame=CGRectMake(100,280,214,50);
[aButtonsetTitle:@"下一页"forState:UIControlStateNormal];
[aButtonaddTarget:selfaction:@selector(buttonTaped:)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview: aButton];
}
//响应下一页按钮的点击事件跳转到下一页,并传值
- (void)buttonTaped:(UIButton*) button{
SecondViewController*secondView = [[SecondViewControlleralloc]init];
//实例化SecondViewController.并将文本输入框的值赋给secondView中的属性
secondView.textString=_aTextField.text;
[selfpresentViewController:secondViewanimated:YEScompletion:nil];
}
@end
演示效果
二、block传值
block传值只是block的一种用法,相比其他传值更高效简单,可以减少代码量,只是刚开始理解起来有点难。
这里我总结了block的传值的用法:
思路:先创建连个视图控制器。第一个控制器创建一个label 和button 控件,其中label用来显示视图控制器回调的值,button用于跳转到第二个视图控制器。
第二个控制器创建两个textField 用于输入文字,在创建两个视图控制器用于跳转到第一个视图控制器
代码:
UserLoginViewController.h
#import<UIKit/UIkit.h>
typedef void(^returnValue)(NSString *user,NSString *pass);/**< 定义一个block */
@interface UserLoginViewController : UIViewController
@property(nonatomic,copy)returnValue value; /**< 定义一个返回值的属性 */
- (void)returnVlaueString:(returnValue)handle;/**< 定义一个返回值的操作,起始这个方法可有可无,定义了这个方法可以方便我们的操作 */
@end
UserLoginViewController.m
#import "UserLoginViewController.h"
@interface UserLoginViewController ()
@property (nonatomic ,strong) UITextField *userName;// 用户名输入框
@property (nonatomic ,strong) UITextField *password; // 密码输入框
@end
@implementation UserLoginViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
// 初始化用户名和密码输入框
_userName = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 214, 50)];
_userName.borderStyle = UITextBorderStyleLine;
[self.view addSubview:_userName];
_password = [[UITextField alloc] initWithFrame:CGRectMake(100, 160, 214, 50)];
_password.borderStyle = UITextBorderStyleLine;
[self.view addSubview:_password];
// 按钮
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(100, 220, 214, 50);
[button setTitle:@"传值" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonTaped:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview: button];
}
// 响应按钮的点击事件
- (void)buttonTaped:(UIButton *) button{
// 将方法回调给上一个页面
self.value(_userName.text,_password.text);
[self dismissViewControllerAnimated:YES completion:nil];
}
// 返回值的方法
-(void)returnVlaueString:(returnValue)handle{
self.value = handle;// 将操作赋给属性
}
@end
viewController.m
#import "ViewController.h"
#import "UserLoginViewController.h"
@interface ViewController ()
@property (nonatomic ,strong) UILabel *label;// 用于显示block回调的值
@property (nonatomic ,strong) UserLoginViewController *userLogin;// 登陆界面
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 214, 50)];
_label.text = @"这里显示block回调的值";
[self.view addSubview:_label];
// 下一页
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(100, 150, 214, 50);
[button setTitle:@"下一页" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonTaped:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
_userLogin = [[UserLoginViewController alloc] init];
// 通过block 回调的值
[_userLogin returnVlaueString:^(NSString *user, NSString *pass) {
_label.text = [NSString stringWithFormat:@"%@,%@",user,pass];
}];
}
// 响应下一页按钮
- (void)buttonTaped:(UIButton *) button{
[self presentViewController:_userLogin animated:YES completion:nil];
}
@end
演示效果:
三、代理传值
1.委托需要做的工作有:
1.1定义协议与方法
1.2声明委托变量
1.3通过委托变量调用委托方法
2.代理需要做的工作有:
2.1遵循协议
2.2实现委托方法
一 .需求:
我们要实现这样一个功能,当点击ViewController视图上的一个按钮的时候立刻跳转到SecondViewController,并在SecondViewController中输入信息,回到ViewController显示
SecondViewController.h
#import <UIKit/UIKit.h>
@class SecondViewController;
// 1.定义协议与方法 一般我们都会在协议后面添加Delegate 这样容易见名知意
@protocol SecondViewDelegate<NSObject>
// 这里模仿苹果官方的写法 格式不是一定固定,后面还可以写你要传递的其他参数// 注意 当方法为@required时是必须实现的方法不然会出现程序崩溃。系统默认 @required
@required
- (void)secondView:(SecondViewController *)SecondView userInfo:(NSString *)userInfo;
// @optional 不是必须实现的方法
@optional
@end
@interface SecondViewController : UIViewController
// 2. 设置委托变量 这里的变量必须遵守协议,才能调用委托方法
@property(nonatomic,strong) id<SecondViewDelegate> delegate;
@end
SecondViewController.h
#import "SecondViewController.h"
@interface SecondViewController ()
@property (nonatomic ,strong) UITextField *userName;// 用户名输入框
@property (nonatomic ,strong) UITextField *password; // 密码输入框
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
// 初始化用户名和密码输入框
_userName = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 214, 50)];
_userName.borderStyle = UITextBorderStyleLine;
[self.view addSubview:_userName];
_password = [[UITextField alloc] initWithFrame:CGRectMake(100, 160, 214, 50)];
_password.borderStyle = UITextBorderStyleLine;
[self.view addSubview:_password];
// 上一页
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(100, 220, 214, 50);
[button setTitle:@"上一页" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonTaped:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
// 响应上一页按钮
- (void)buttonTaped:(UIButton *) button{
// 委托调用代理方法 这里判断了是否存在代理和代理方法
if (_delegate && [_delegate respondsToSelector:@selector(secondView:userInfo:)]) {
// 调用代理方法传递文本输入框内容
[_delegate secondView:self userInfo:[NSString stringWithFormat:@"%@,%@", _userName.text,_password.text]];
}
[self dismissViewControllerAnimated:YES completion:nil];
}
@end
ViewController.m
#import "ViewController.h"
#import "SecondViewController.h"
// 导入协议
@interface ViewController () <SecondViewDelegate>
@property (nonatomic ,strong) UILabel *label;// 用于显示值
@property (nonatomic ,strong) SecondViewController *secondView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 214, 50)];
_label.text = @"这里显示代理回调的值";
[self.view addSubview:_label];
// 下一页
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(100, 150, 214, 50);
[button setTitle:@"下一页" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonTaped:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
// 第二个页面
_secondView = [[SecondViewController alloc] init];
// 1、遵守协议 _secondView 中的delegate代理属性,让本视图遵守这个协
_secondView.delegate = self;
}
// 响应下一页按钮
- (void)buttonTaped:(UIButton *) button{
// 跳转到下一页
[self presentViewController:_secondView animated:YES completion:nil];}
#pragma mark - <SecondViewDelegate> 实现委托方法
- (void)secondView:(SecondViewController *)SecondView userInfo:(NSString *)userInfo{
// 将传递过来的值显示
_label.text = userInfo;
}
@end
演示效果
四、单例传值
五、通知传值(自定义传值)
六、NSUserDefaults(存储传值)
七、全局传值(AppDelegate)