UI控件笔记(七):UI之五种传值方式 代理\单例\缓存\通知\正向
来源:互联网 发布:mac 思维导图 免费 编辑:程序博客网 时间:2024/06/04 08:55
一、传值简介
AAAA代理: 1V1 A->B,B回A的时候使用
BBBB 单例: 无所谓 整个应用都要用的数据,存一个应用内的全局值,ios6?ios7
CCCC 缓存:为所谓 整个应用都要用的数据,保证下次启动还有点数据,账号、密码
DDDD 通知: 1V多 犯懒的时候,A发通知,B观察通知,B在A之前,比较占资源
E 正向传值:A->B,B的内容由A来定,那么B应该有一个属性,A->B的时候给B的属性赋值
二、举例
1、AppDelegate.m文件中的写法
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindowalloc] initWithFrame:[UIScreenmainScreen].bounds];
self.window.backgroundColor = [UIColorwhiteColor];
FirstViewController *first = [[FirstViewControlleralloc] init];
UINavigationController *nav = [[UINavigationControlleralloc] initWithRootViewController:first];
[first release];
self.window.rootViewController = nav;
[nav release];
[self.windowmakeKeyAndVisible];
return YES;
}
2、FirstViewController.m文件中的写法
#import "FirstViewController.h"
#import "SecondViewController.h"
#import "SingleTon.h"
@interface FirstViewController ()<SecondDelegate>//AAAA代理声明可以写这里
@end
@implementation FirstViewController
- (void)viewDidLoad {
[superviewDidLoad];
self.view.backgroundColor = [UIColorredColor];
//DDDD 通知中心
[[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(notify:)name:@"pp"object:nil];
//第三个参数:这里专门等着接的通知名
//第一个和第二个参数:截到对应通知名的通知后,用第一个参数去执行第二个参数的方法
// Do any additional setup after loading the view.
}
-(void)notify:(NSNotification*)noti
{
UIColor *color = [notiobject];//发通知的时候传过的参数,就是这里的这个noti,无论什么类型的参数,都用object去解析,解析完了,发的时候是什么类型,就是什么类型
self.view.backgroundColor = color;
}
//touch事件,去第二个页面
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"松手了");
SecondViewController *second = [[SecondViewControlleralloc] init];
second.delegate =self;//AAAA被触发者的第二件事儿
[self.navigationControllerpushViewController:second animated:YES];
[second release];
}
//AAAA 实现代理的方法
-(void)getColor:(UIColor *)color
{
self.view.backgroundColor = color;
}
-(void)viewWillAppear:(BOOL)animated
{
static int num =0;
if(num != 0)
{
[self single];
[self userdefaults];
}
num++;
}
-(void)single//BBBB单例
{
SingleTon *single = [SingleTonshareInstance];//还是那个
self.view.backgroundColor = single.color;
}
-(void)userdefaults//CCCC缓存
{
NSArray *colorArr = @[[UIColor redColor],[UIColor yellowColor],[UIColor blueColor]];//数组存三色
int num = [[[NSUserDefaultsstandardUserDefaults] objectForKey:@"color"]intValue];
self.view.backgroundColor = colorArr[num];
}
3、SecondViewController.h文件中的写法
#import <UIKit/UIKit.h>
AAAA 声明代理
@protocol SecondDelegate <NSObject>
-(void)getColor:(UIColor*)color;
@end
@interface SecondViewController : UIViewController
@property(nonatomic,assign)id<SecondDelegate>delegate;
@end
4、SecondViewController.m文件中的写法
#import "SecondViewController.h"
#import "SingleTon.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (void)viewDidLoad {
[superviewDidLoad];
self.view.backgroundColor = [UIColorgreenColor];
[self makeBtn];
// Do any additional setup after loading the view.
}
-(void)makeBtn
{
NSArray *arr =@[@"red",@"yellow",@"blue"];
for(int i =0;i<arr.count;i++)
{
UIButton *btn = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(0, 64+i*60, 320,60);
[btn setTitle:arr[i]forState:UIControlStateNormal];
[btn addTarget:selfaction:@selector(btnDown:)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:btn];
btn.tag = 1000+i;
}
}
-(void)btnDown:(UIButton*)btn
{
NSArray *colorArr = @[[UIColor redColor],[UIColor yellowColor],[UIColor blueColor]];//数组存三色
//该传值了
[self delegateMethod:colorArr[btn.tag -1000]]; AAAA
[self singleMethod:colorArr[btn.tag -1000]]; BBBB
[self userdefaultsMethod:btn.tag -1000]; CCCC
[self notificationMethod:colorArr[btn.tag -1000]]; DDDD
[self.navigationControllerpopViewControllerAnimated:YES];
}
-(void)delegateMethod:(UIColor*)color//AAAA代理
{
[self.delegategetColor:color];
}
-(void)singleMethod:(UIColor*)color//BBBB单例
{
SingleTon *single = [SingleTonshareInstance];
single.color = color;//给单例的属性赋值
}
-(void)userdefaultsMethod:(int)color//CCCC缓存
{
NSUserDefaults *user = [NSUserDefaultsstandardUserDefaults];
[user setObject:[NSStringstringWithFormat:@"%d",color]forKey:@"color"];
[user synchronize];
}
-(void)notificationMethod:(UIColor*)color//DDDD通知
{
//1、做一个通知中心
NSNotificationCenter *center = [NSNotificationCenterdefaultCenter];
//2、做一个通知内容
NSNotification *notify = [NSNotificationnotificationWithName:@"pp"object:color];//第一个参数:通知名,第二个参数:通知携带的参数
//3、用通知中心把通知内容post出去,然后就不管了
[center postNotification:notify];//中心调方法,内容是参数
}
5、SingleTon.h单例写法 BBBB
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface SingleTon : NSObject
@property(nonatomic,retain)UIColor *color;//用来保存全局颜色的
+(id)shareInstance;
@end
6、SingleTon.m单例写法 BBBB
#import "SingleTon.h"
static SingleTon *single =nil;
@implementation SingleTon
+(id)shareInstance
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
single = [SingleTonalloc];//这里只走一次
});
return single;
}
@end
- UI控件笔记(七):UI之五种传值方式 代理\单例\缓存\通知\正向
- UI控件笔记(一):UI之UIView的属性
- UI控件笔记(二):UI之UILabel和UIImageView
- UI控件笔记(三):UI之UIButton的属性
- UI控件笔记(四):UI之UITextField的属性
- UI控件笔记(五):UI之UIViewController和NSUserDefaults
- UI控件笔记(九):UI之UITabBarController
- UI控件笔记(十三):UI之MVC
- UI控件笔记(十七):UI之UI阶段控件常用方法大全总结
- UI控件代理属性
- iOS笔记UI--普通传值(正向传值)
- UI之单例模式
- iOS 之单例 代理 通知
- UI控件笔记(六):UI之UINavigationController、navigationBar和UIView的封装
- UI控件笔记(八):UI之UISlider\UIStepper\ UIProgressView\ UITextView\UISwitch\ UIActivityIndicatorView\ UISeg
- UI控件笔记(十一):UI之UITableView\自定义UITableView\分组
- UI控件笔记(十二):UI之TableEdit\ UISearchBar\自定义实时搜索\ 索引
- UI控件笔记(十四):UI之自定义导航条的封装
- WebAPI 和 webservice的区别
- ubuntu14.04 root login
- 东南大学2016年考博信息汇总
- Javascript 严格模式详解
- 小米兰亭字体_下载
- UI控件笔记(七):UI之五种传值方式 代理\单例\缓存\通知\正向
- Push时间过短引起程序Crash
- 数据库表分割技术浅析(水平分割/垂直分割/库表散列)
- 在 Oculus和 Gear VR上开发跨平台的 VR应用
- Android下拉刷新上拉加载控件,对所有View通用!
- 计算机视觉(Computer Vision)基本概念
- [Android] 带有输入框(EditText)的提示框(AlertDialog)
- 各种开源UI
- 系统之间通讯方式—SOAP(web service)