iOS-传值大全

来源:互联网 发布:adams导入数据 编辑:程序博客网 时间:2024/06/05 12:00

前言

iOS传值方式很多,这里面我主要讲几种常用的传值;

以下传值正向传值A页面到B页面,反向传值B页面传到A页面。

1.属性传值

A页面:

SecondViewController *svc = [[SecondViewController alloc]init];    UINavigationController *nvc = [[UINavigationController alloc]initWithRootViewController:svc];    svc.passValue = @"这是传的值";    [self presentViewController:nvc animated:YES completion:nil];

B页面:

先声明一个属性:

@interface SecondViewController : UIViewController@property (nonatomic,copy) NSString *passValue;@end
使用传的值

UILabel *test = [[UILabel alloc]init];    test.textAlignment = NSTextAlignmentCenter;    test.backgroundColor = [UIColor colorWithRed:0.961 green:0.961 blue:0.961 alpha:1.00];    test.frame = CGRectMake((ScreenWidth - 200)/2, (ScreenHeight - 40)/2, 200, 40);    test.text = _passValue;    [self.view addSubview:test];

2.文件传值

A页面:

NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];    [ud setObject:self.navigationItem.title forKey:@"passValue"];    [ud synchronize];

B页面:

NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];        NSString *str = [ud objectForKey:@"passValue"];        NSLog(@"str == %@",str);

3.代理传值

B页面设置代理

@protocol ChangeValueDelegate <NSObject>- (void)changeValueWithPassValue:(NSString *)passValue;@end@interface DetailViewController : UIViewController@property (assign, nonatomic) id<ChangeValueDelegate> delegate;@end
赋值

if ([self.delegate respondsToSelector:@selector(changeValueWithPassValue:)]) {            [self.delegate changeValueWithPassValue:@"这是传的值"];        }
A页面遵循代理

@interface RootViewController ()<ChangeValueDelegate>
SecondViewController *dvc = [[SecondViewController alloc] init];    dvc.delegate = self;    [self.navigationController pushViewController:dvc animated:YES];
现实代理

- (void)changeValueWithPassValue:(NSString *)titleStr{    NSLog(@"titleStr == %@",titleStr);}

4.广播传值

A页面发送通知

NSDictionary *dict = [NSDictionary dictionaryWithObject:@"我是传的值" forKey:@"value"];    [[NSNotificationCenter defaultCenter] postNotificationName:@"KPassVaule" object:nil userInfo:dict];
B页面接收通知

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getPassVauleget:) name:@"KPassVaule" object:nil];
- (void)getPassVauleget:(NSNotification *)notifi{    NSString *str = [notifi.userInfo objectForKey:@"value"];    NSLog(@"str == %@",str);}

5.KVO传值

在初始化方法中加入

[_stu addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];

在dealloc中移除KVO监听:

[_stu removeObserver:self forKeyPath:@"name" context:nil];

添加默认的响应回调方法:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{    if ([object isKindOfClass:[Student class]]) {        NSLog(@"_stu-old:%@",[change objectForKey:@"old"]);        NSLog(@"_stu-new:%@",[change objectForKey:@"new"]);    }}

6.blcok反向传值

A页面

DetailViewController *dvc = [[DetailViewController alloc] init];    dvc.fontSize = _label.font.pointSize;    void (^mBlocks)(float value);    mBlocks = ^(float value){        _label.font = [UIFont systemFontOfSize:value];    };    [dvc changeFont:mBlocks];    [self presentViewController:dvc animated:YES completion:^{        NSLog(@"hb");    }];

B页面

#import <UIKit/UIKit.h>@interface DetailViewController : UIViewController{    void (^_mBlock)(float value);}@property (assign, nonatomic) float fontSize;- (void)changeFont:(void (^)(float value))mBlock;@end

传值

_mBlock(1111.00);


希望能帮助到你!微笑





0 0