iOS-传值大全

前言

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页面:

先声明一个属性:

@property (nonatomic,copy) NSString *passValue;  

使用传的值

NSLog(@"%@",_passValue);

2.文件传值

A页面:

NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];  
[ud setObject:@"这是传的值" 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:(voidvoid *)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);


转载请注明出处http://blog.csdn.net/chen_gp_x

发布了110 篇原创文章 · 获赞 124 · 访问量 100万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 撸撸猫 设计师: 设计师小姐姐

分享到微信朋友圈

×

扫一扫,手机浏览