iOS传值

写给自己做笔记  不足之处 请多多指教

1.代理传值

在a触发代理  b会帮忙实现 

在ViewController.h文件中  最上面 import下面

@protocol PassTrendValueDelegate

-(void)passTrendValues:(NSString *)values;//1.1定义协议与方法

@end

声明属性

@property (retain,nonatomic) id<PassTrendValueDelegate>trendDelegate;

.m文件中  创建一个按钮用来响应 代理

UIButton *btn = [[UIButton alloc]initWithFrame:(CGRect){200,200,200,200}];

[self.view addSubview:btn];

[btn setBackgroundColor:[UIColor redColor]];

[btn addTarget:self action:@selector(trendBtnClick) forControlEvents:UIControlEventTouchUpInside];

#pragma mark 点击趋势按钮

-(void)trendBtnClick{

//create the view

TwoViewController *trendViewController = [[TwoViewController alloc] init];

self.trendDelegate= trendViewController; //设置代理

[self.trendDelegate passTrendValues:@"Lves李兴乐"];

}

TwoViewController.m文件中  添加协议PassTrendValueDelegate

实现代理方法

-(void)passTrendValues:(NSString *)values{

NSLog(@"代理在这里  values:::%@",values);

}

2.属性传值

a传b

b.h文件中添加属性

a跳转时直接赋值即可

3.通知传值

传值

NSNotification *notice = [NSNotification notificationWithName:@"number" object:nil userInfo:@{@"last":la}];

[[NSNotificationCenter defaultCenter]postNotification:notice];

接受

NSNotificationCenter *center = [NSNotificationCenter defaultCenter];

[center addObserver:self selector:@selector(notice:) name:@"number" object:nil];

4.单利

声明一个类 .h文件中 申明要传的属性以及调用方法

+(ChapterManager *)chapterManager;

@property (nonatomic,strong) NSMutableArray *neet_datas;

.m文件中 实现

static ChapterManager *manager = nil;

@implementation ChapterManager

+(ChapterManager *)chapterManager

{

if (manager == nil) {

manager = [[ChapterManager alloc]init];

}

return manager;

}

调用  类名直接调用

block 不太会

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容