Objective-C学习笔记之传值

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)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,457评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,837评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,696评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,183评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,057评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,105评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,520评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,211评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,482评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,574评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,353评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,213评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,576评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,897评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,489评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,683评论 2 335

推荐阅读更多精彩内容