一。UINavigationController导航控制器
1.创建导航控制器,首先在AppDelegate上添加,
self.window.rootViewController = [[UINavigationControlleralloc] initWithRootViewController:[ViewControllernew]];
确定在根视图控制器上,添加导航控制器。
2.导航条上分3部分左右按钮,加中间视图,自己选择添加
//通过文字设置导航条左边按钮
//设置当前导航条左边或者右边中间按钮必须用self.navigationItem
// self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"文字" style:UIBarButtonItemStylePlain target:self action:@selector(printf:)];
//通过系统设置导航条左边按钮
self.navigationItem.leftBarButtonItem = [[UIBarButtonItemalloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearchtarget:selfaction:@selector(printf:)];
//通过图片设置导航条右边按钮
// self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Fav_Filter_ALL_HL@2x"] style:UIBarButtonItemStylePlain target:self action:@selector(imgNamed:)];
//设置中间自定义视图
// self.navigationItem.title = @"hello";
// self.title = @"world";
UITextField *textF = [[UITextFieldalloc] initWithFrame:CGRectMake(0,0, 200, 30)];
textF.borderStyle =UITextBorderStyleRoundedRect;
self.navigationItem.titleView = textF;
//设置导航条右边按钮自定义
UIButton *button = [UIButtonbuttonWithType:UIButtonTypeCustom];
frame = CGRectMake(0,0, 50, 30);
[button setImage:[UIImageimageNamed:@"001@2x"]forState:UIControlStateNormal];
imageEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
self.navigationItem.rightBarButtonItem = [[UIBarButtonItemalloc] initWithCustomView:button];
//设置导航条返回按钮
self.navigationItem.backBarButtonItem = [[UIBarButtonItemalloc] initWithTitle:@"返回"style:UIBarButtonItemStylePlain target:nil action:nil];
//设置导航条的背景色
[self.navigationBarsetBarTintColor:[UIColororangeColor]];
3.导航控制器的跳转
a.跳转过用pushViewController
例子:self.navigationItem.rightBarButtonItem = [[UIBarButtonItemalloc]initWithTitle:@"aaa"style:UIBarButtonItemStylePlain target:self action:@selector(push)];
-(void)push{
SecondViewController *svc = [[SecondViewControlleralloc]init];//创建一个新视图
[self.navigationControllerpushViewController:svc animated:nil];//用pushViewController跳转
}
b.跳转回来用[self.navigationControllerpopViewControllerAnimated:YES];
//回到根视图控制器
// [self.navigationController popToRootViewControllerAnimated:YES];
/回到指定视图控制器
[self.navigationControllerpopToViewController:vc animated:YES];
//回到上一层视图控制器
[self.navigationControllerpopViewControllerAnimated:YES];
=======================
二。UITouch
//触摸开始
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent
{
//获取坐标信息
//获取到触摸点
UITouch *touch = [touches anyObject];
//转化为当前视图的具体坐标
CGPoint point = [touch locationInView:self.view];
NSLog(@"point == %@",NSStringFromCGPoint(point));
}
//触摸移动
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent
{
//获取坐标信息
//获取到触摸点
UITouch *touch = [touches anyObject];
//转化为当前视图的具体坐标
CGPoint point = [touch locationInView:self.view];
NSLog(@"point == %@",NSStringFromCGPoint(point));
}
//触摸结束
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent
{
//获取坐标信息
//获取到触摸点
UITouch *touch = [touches anyObject];
//转化为当前视图的具体坐标
CGPoint point = [touch locationInView:self.view];
NSLog(@"point == %@",NSStringFromCGPoint(point));
}
//触摸取消
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent
{
//获取坐标信息
//获取到触摸点
UITouch *touch = [touches anyObject];
//转化为当前视图的具体坐标
CGPoint point = [touch locationInView:self.view];
NSLog(@"point == %@",NSStringFromCGPoint(point));
}
=======================
三。通知中心
1.什么是通知中心
通知是一种发送给一个或者多个观察者,用来通知其在程序中发生了某个事件的消息。通知机制的核心就是一个进程中单一实例的对象,被叫做通知中心(NSNotificationCenter)。当一个对象发布一个通知时,通知会先被发布 到通知中心。通知中心的作用相当于是交流所,作为通知的广播中心。程序中其他需要感知该事件的对象通过向通知中心注册就可以达到在事件发生时被通知中心及 时通知到得目的。
2.如何注册
- (void)addObserver:(id)observer selector:(SEL)aSelector name:(NSString *)aName object:(id)anObject;
//1.通知中心频道必须保持一致
//2.必须先注册通知中心才能发送通知
//3.通知中心不是UI控件,所以需要视图控制器被销毁的时候移除
//添加通知中心
//参数3:频道
//参数4:形参
self selector:@selector(changeValue:) name:@"hello" object:nil];
3.如何发送通知
- (void)postNotificationName:(NSString *)aName object:(id)anObject;
//发送通知
// [[NSNotificationCenter defaultCenter] postNotificationName:@"hello" object:@"发送消息"];
//带一个参数
[[NSNotificationCenterdefaultCenter] postNotificationName:@"hello"object:nil];
NSMutableDictionary *dictM = [NSMutableDictionarydictionaryWithObject:@"1"forKey:@"one"];
//带两个参数
[[NSNotificationCenterdefaultCenter] postNotificationName:@"hello"object:@"object消息"userInfo:dictM];
4.移除通知中心
- (void)removeObserver:(id)observer name:(NSString *)aName object:(id)anObject;
5.通知中心与代理,Block区别
代理,Block: 一对一
通知中心: 一对多
使用通知中心会提高程序的耦合度.
=======================
四。NSUserDefaults沙盒
1.NSUserDefaults作用
NSUserDefaults对象是用来保存,恢复应用程序相关的偏好设置,配置数据等等
应用场景:
储存APP用户的账户和密码
2.识别是否为第一次使用该APP
2.具体用法
’ //存储(不同数据类型不同储存方式)
[[NSUserDefaultsstandardUserDefaults] setObject:@"hello"forKey:@"world"];
// [[NSUserDefaults standardUserDefaults] setInteger:@"" forKey:@""]
// [NSUserDefaults standardUserDefaults] setBool:<#(BOOL)#> forKey:<#(NSString *)#>
//闪存同步数据
[[NSUserDefaultsstandardUserDefaults] synchronize];
//沙河家目录
NSLog(@"home == %@",NSHomeDirectory());
//读取数据
// NSString *str = [[NSUserDefaults standardUserDefaults] objectForKey:@"world"];
//删除
[[NSUserDefaultsstandardUserDefaults] removeObjectForKey:@"world"];
=======================
五。UITabBarController(分栏控制器)
1.如何创建分栏控制器
//第一页v1里面
//想让第一页天生自带导航栏天赋,就在v1创建一个带导航栏的控制器,并将nav存入分栏中。
UINavigationController *nav = [[UINavigationControlleralloc] initWithRootViewController:v1];
UITabBarController *tabbar = [[UITabBarControlleralloc] init];
//分栏控制器装载所有视图控制器和导航控制器
//tabBarController中最多只允许显示5个视图控制器
//超出的系统会自动添加一个more
v1.title = @"红色";
v1.view.backgroundColor = [UIColorredColor];
title = @"蓝色";
v2.view.backgroundColor = [UIColorblueColor];
title = @"绿色";
v3.view.backgroundColor = [UIColorgreenColor];
title = @"黄色";
v4.view.backgroundColor = [UIColoryellowColor];
title = @"灰色";
v5.view.backgroundColor = [UIColorgrayColor];
title = @"橙色";
v6.view.backgroundColor = [UIColororangeColor];
viewControllers = @[nav,v2,v3,v4,v5,v6];
//设置代理对象
delegate = self;
//隐藏tabbar
// tabbar.tabBar.hidden = YES;
//设置选中tabbar的下标默认选中数组的第0个元素
// tabbar.selectedIndex = 1;
————————————代理方法—————————————
//被选中的时候
- (void)tabBarController:(UITabBarController
UIViewController
{
//viewController当前点击的视图控制器
NSLog(@"title == %@",viewController.title);
//tabBarController.selectedIndex当前点击tabbarController的下标
NSLog(@"index == %ld",tabBarController.selectedIndex);
}
//不能被选中
- (BOOL)tabBarController:(UITabBarController
shouldSelectViewController:(UIViewController
{
// if ([viewController.title isEqualToString:@"蓝色"]) {
// return NO;
// }
return YES;
}
//下面这三个方法主要用于监测对moreViewController中对view controller的edit操作
//即将改变视图控制器的位置
- (void)tabBarController:(UITabBarController
willBeginCustomizingViewControllers:(NSArray
{
NSLog(@"即将改变视图控制器的位置");
}
//即将结束编辑状态
- (void)tabBarController:(UITabBarController
willEndCustomizingViewControllers:(NSArray
BOOL)changed
{
NSLog(@"即将结束编辑状态");
//change代表是否改变视图控制器的位置
if
for(UIViewController *vcin
{
NSLog(@"vc.title == %@",vc.title);
}
}
}
//已经结束编辑状态
- (void)tabBarController:(UITabBarController
didEndCustomizingViewControllers:(NSArray
BOOL)changed
{
NSLog(@"已经结束编辑状态");
}
/*——————————————自定义分栏控制器—————————————————————*/
由于系统的分栏控制器不能调整高度和更换图案,所以要自定义分栏控制器
使用原理:首先在要设置分栏的地方创建一个UIImageView,在view上添加按钮,每个按钮对应每一个分栏控制器,当选择其中一个按钮的时候,返回一个tag给分栏控制器,tag对应第几张控制器。然后将该控制器设为选择。
主要的类为:1.分栏控制器类,2图片控制器
1.分栏控制器类
//1.隐藏系统的tabbar //系统tabbar高度49
self.tabBar.hidden =YES;
//2.自定义tabbar
self.customView = [[CustomViewalloc] initWithFrame:CGRectMake(0,self.view.frame.size.height -60, self.view.frame.size.width,60)];
//点击按钮回调视图控制器在tabbarcontroller中的位置
//index代表图控制器tabbarcontroller中的下标
//block实现里面是不能用self因为不会对象不会被释放 内存泄露
__weak typeof(self) safeSelf =self;
self.customViewsetBlock:^(NSInteger
selectedIndex
}];
self.viewaddSubview:self.customView];
ViewController1 *v1 = [[ViewController1alloc] init];
v1.view.backgroundColor = [UIColorredColor];
UINavigationController *nav = [[UINavigationControlleralloc] initWithRootViewController:v1];
ViewController2 *v2 = [[ViewController2alloc] init];
v2.view.backgroundColor = [UIColororangeColor];
ViewController3 *v3 = [[ViewController3alloc] init];
v3.view.backgroundColor = [UIColoryellowColor];
ViewController4 *v4 = [[ViewController4alloc] init];
v4.view.backgroundColor = [UIColorblueColor];
self.viewControllers =@[nav,v2,v3,v4];
2.图片控制器
//设置图片的位置
- (instancetype)initWithFrame:(CGRect)frame
{
self = [superinitWithFrame:frame];
if (self) {
self.image = [UIImageimageNamed:@"tabbar_bg@2x"];
//打开交互
self.userInteractionEnabled =YES;
//创建tabbarItem按钮
self createButton:frame];
}
return self;
}
//设置图片上button的位置
- (void)createButton:(CGRect)frame
{
for(int i =0; i < 4; i++)
{
UIButton *button = [UIButtonbuttonWithType:UIButtonTypeCustom];
frame = CGRectMake(frame.size.width /4 * i, 0, frame.size.width /4, frame.size.height);
[button setImage:[UIImageimageNamed:[NSStringstringWithFormat:@"tab%d_n@2x",i+1]]forState:UIControlStateNormal];
[button setImage:[UIImageimageNamed:[NSStringstringWithFormat:@"tab%d_s@2x",i+1]]forState:UIControlStateSelected];
if (i == 0) {
selected =YES;
}
tag = 100
[button addTarget:selfaction:@selector(changeView:)forControlEvents:UIControlEventTouchUpInside];
self addSubview:button];
}
}
//按钮的点击事件,每个事件对应不同的分栏
- (void)changeView:(UIButton
{
for(int i =0; i < 4; i++)
{
UIButton *btn = (UIButton *)[selfviewWithTag:100
selected = NO;
}
selected = YES;
//每个按钮的tag值对应不同视图控制器通过tag来标记视图控制器
self.block(button.tag -100);
}
六。UIScorllView
使用原理和简单定义:可以将UIScorllView想象成一张大纸,然后在纸上放置图片,通过拖动或其他方式浏览,使用例子,如广告栏之类的。
使用步骤
//设置滚动内容的大小
contentSize = CGSizeMake(WIDTH * 3, HEIGHT * 3);
for(int i = 0; i < 3; i++)
{
for(int j =0; j < 3; j++)
{
UIView *view = [[UIViewalloc] initWithFrame:CGRectMake(WIDTH * j,HEIGHT *i, WIDTH, HEIGHT)];
view.backgroundColor = [UIColorcolorWithRed:arc4random() % 256 / 255.0green:arc4random() % 256 /255.0 blue:arc4random() %256 / 255.0alpha:1];
addSubview:view];
}
}
//是否分页滚动
pagingEnabled = YES;
//设置内容位置 scrollview的偏移量
// [scrollView setContentOffset:CGPointMake(WIDTH, HEIGHT)];
//是否显示水平滚动条
scrollView.showsHorizontalScrollIndicator =NO;
//是否显示垂直滚动条
scrollView.showsVerticalScrollIndicator =NO;
//弹簧效果
bounces = YES;
//设置代理对象
delegate = self;
[NSTimerscheduledTimerWithTimeInterval:1.0target:selfselector:@selector(imgViewMove:)userInfo:scrollView repeats:YES];
- (void)imgViewMove:(NSTimer
{
UIScrollView *view = (UIScrollView *)timer.userInfo;
[view setContentOffset:CGPointMake(WIDTH,0) animated:YES];
}
#pragma mark -- UIScrollViewDelegate 代理方法
//即将开始拖拽
- (void)scrollViewWillBeginDragging:(UIScrollView
{
NSLog(@"point == %@",NSStringFromCGPoint(scrollView.contentOffset));
}
//视图停止时调用
- (void)scrollViewDidEndDecelerating:(UIScrollView
{
NSLog(@"point == %@",NSStringFromCGPoint(scrollView.contentOffset));
}
//视图动画结束时调用
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView
{
NSLog(@"视图动画结束时调用");
}
//拖动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
// NSLog(@"point == %@",NSStringFromCGPoint(scrollView.contentOffset));
}
//即将停止拖拽
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout
{
// NSLog(@"point == %@",NSStringFromCGPoint(scrollView.contentOffset));
}
//已经停止拖拽
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
// NSLog(@"point == %@",NSStringFromCGPoint(scrollView.contentOffset));
}
//即将减速
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
// NSLog(@"point == %@",NSStringFromCGPoint(scrollView.contentOffset));
}
//点击状态栏回到顶部
- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView
{
return YES;
}
//已经滑到顶部
- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView
{
// NSLog(@"已经滑到顶部");
}