传值
来源:互联网 发布:java读写分离 编辑:程序博客网 时间:2024/05/19 18:14
———-KVO传值———
ViewController.m:
#import "ViewController.h"
@interface ViewController ()
{
UIScrollView *myscrollView;
UILabel *label;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
myscrollView = [[UIScrollView alloc]initWithFrame:self.view.frame];
myscrollView.contentSize = CGSizeMake(0, 800);
myscrollView.backgroundColor = [UIColor redColor];
[self.view addSubview:myscrollView];
// kvo得到滚动视图的内容偏移量
[myscrollView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];//观察者的名字 如:myscrollView 观察者的属性名字:contentOffset
label = [[UILabel alloc]initWithFrame:CGRectMake(100, 200, 170, 50)];
label.backgroundColor = [UIColor whiteColor];
label.textColor = [UIColor blueColor];
label.textAlignment = NSTextAlignmentCenter;
[myscrollView addSubview:label];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
if ([keyPath isEqualToString:@"contentOffset"]) {
CGPoint set = [change [@"new"]CGPointValue];
NSLog(@"%f",set.y);
label.text = [NSString stringWithFormat:@"坐标:%f",set.y];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
————通知传值1————
HTTPRequster.h
#import <Foundation/Foundation.h>
@interface HTTPRequster : NSObject
+(void)getRequset;
@end
HTTPRequster.m:
#import "HTTPRequster.h"
@implementation HTTPRequster
+(void)getRequset
{
NSDictionary *dic = @{@"name":@"小葵葵",@"tel":@"18798032338"};
[[NSNotificationCenter defaultCenter]postNotificationName:@"didResponse" object:nil userInfo:dic];//通知中心
}
@end
ViewController.m
#import "ViewController.h"
#import "HTTPRequster.h"
@interface ViewController ()
{
UILabel *label;
}
@end
@implementation ViewController
- (instancetype)init{
self = [super init];
if (self) {
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(reloadDataMassage:) name:@"didResponse" object:nil];
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
label = [[UILabel alloc]initWithFrame:CGRectMake(100, 200, 150, 50)];
label.textColor = [UIColor redColor];
label.backgroundColor = [UIColor whiteColor];
label.textAlignment = NSTextAlignmentCenter;
label.numberOfLines = 2;
[self.view addSubview:label];
[HTTPRequster getRequset];
}
- (void)reloadDataMassage:(NSNotification *)not
{
NSLog(@"%@",not.userInfo);
label.text = [NSString stringWithFormat:@"%@\n%@",not.userInfo[@"name"],not.userInfo[@"tel"]];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
———-通知传值2-———-
———-反向传值———viewcontroller.m—————————
#import "ViewController.h"
#import "V_1ViewController.h"
@interface ViewController ()
{
UILabel *lable1;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor greenColor];
lable1 = [[UILabel alloc]initWithFrame:CGRectMake(150, 200, 150, 50)];
lable1.backgroundColor = [UIColor redColor];
lable1.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:lable1];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeCity:) name:@"select" object:nil];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(next)];
[self.view addGestureRecognizer:tap];
}
- (void)changeCity:(NSNotification *)not
{
NSDictionary *info = not.userInfo;
NSString *cityname = info[@"city"];
lable1.text = cityname;
}
-(void)next{
V_1ViewController *v1 = [[V_1ViewController alloc]init];
[self.navigationController pushViewController:v1 animated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
——————————v_1viewcontroller———————————
#import "V_1ViewController.h"
#import "V_2ViewController.h"
@interface V_1ViewController ()
{
UILabel *lable1;
}
@end
@implementation V_1ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor blueColor];
lable1 = [[UILabel alloc]initWithFrame:CGRectMake(150, 200, 150, 50)];
lable1.backgroundColor = [UIColor orangeColor];
[self.view addSubview:lable1];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(next)];
[self.view addGestureRecognizer:tap];
}
- (void)next
{
V_2ViewController *v2 = [[V_2ViewController alloc]init];
[self.navigationController pushViewController:v2 animated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
——————————-V_2viewcontroller——————————
#import "V_1ViewController.h"
#import "V_2ViewController.h"
@interface V_1ViewController ()
{
UILabel *lable1;
}
@end
@implementation V_1ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor blueColor];
lable1 = [[UILabel alloc]initWithFrame:CGRectMake(150, 200, 150, 50)];
lable1.backgroundColor = [UIColor orangeColor];
[self.view addSubview:lable1];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer
alloc]initWithTarget:self action:@selector(next)];
[self.view addGestureRecognizer:tap];
}
- (void)next
{
V_2ViewController *v2 = [[V_2ViewController alloc]init];
[self.navigationController pushViewController:v2 animated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
—————-block传值1—————-
主要是观察其执行的顺序
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 申明:
// 返回值类型(^闭包的名字)(参数列表)
// 实现: 闭包 = ^(参数列表){实现的内容}
// 调用:
// 闭包名字 ();
//
顺序: 1. void (^qqq)(int num, NSString *string);
顺序: 2. qqq = ^(int num,NSString *string)
{
NSLog(@"%d%@",num,string);
};
顺序: 3. qqq (111,@"sb");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
—————-block传值2—————-
—————-viewcontroller.m
#import "ViewController.h"
#import "V_1ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
V_1ViewController *v1 = [[V_1ViewController alloc]init];
[v1 tou:^(NSString *message) {
NSLog(@"%@吃小鸡腿",message);
}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
————-V_1ViewController.h
#import <UIKit/UIKit.h>
@interface V_1ViewController : UIViewController
- (void)tou:(void(^)(NSString *message))block;
@end
————-V_1ViewController.m
#import "V_1ViewController.h"
@interface V_1ViewController ()
@end
@implementation V_1ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)tou:(void(^)(NSString *message))block{
block(@"小奎子");
}
- 传值
- 传值
- 传值
- 传值
- 传值
- 传值
- 传值
- 传值
- 传值
- 传值
- 传值
- 传值
- 传值
- 传值、传引用、传值
- iOS传值(block传值)
- IOS传值:block传值
- c#传值
- JavaScript传值
- Opencv向下采样-pyrDown函数
- jsp作用域
- Axure之基本使用
- Opencv形态学滤波-闭运算
- Memcache应用场景介绍,说明[zz]
- 传值
- iOS开发博客收集
- JSP内置对象,作用域
- SecurityManager在tomcat中的使用(翻译)
- 自定义控件+ViewPage+Fragment....各种收获
- Opencv形态学滤波-顶帽
- 视频
- php set get方法使用
- Spark(十二)--性能调优篇