大纲
一、作业Homework_CircleMove0314
理论:不可在分线程中刷新UI,否则无法实现预期界面效果。
二、UINavigationController
项目:UINavigationController0315
理论:
栈:后进先出 队列:先进先出
导航(NavController):继承于UIViewController,根据栈的原理,实现多个视图的之间的切换功能。
界面跳转方法共4种:①root②模态弹出③
④UINavigationController:
步骤:
1.创建ViewController
2.创建NavigationController,且设置vc为nav的root
3.将nav设置为window的root
进栈:pushViewController: animated:
出栈:pop
方法1:(到上一vc)popViewController
方法2:(到root)popToRootViewController
方法3:(到任意vc)popToViewController
三、Navigation_Title_Button
项目:Navigation_Title_Button0315
理论:
1.显示导航栏标题
1.1 方法1:self.title
1.2 方法2:self.navigationItem.title
2.添加导航按钮
2.1 系统提供的方式
①initWithBarButtonSystemItem: target: action:
②initWithTitle: style: target: action:
③initWithImage: style: target: action:
2.2 自定义方式
步骤:
1.创建button,设置其bounds/action等
2.声明UIBarButtonItem,并initWithCustomView:button
3.将button添加到nav
四、隐藏导航栏
项目:NavigationBar0315
理论:
设置导航隐藏:self.navigationController.navigationBarHidden = YES;
位置:一般设置在viewWillAppear:
五、使用storyBoard创建导航栏
项目:Navigation_UseStoryBoard0315
理论:
1.选中storyboard
2.点击Editor→Embed In→Navigation Controller
正文
一、作业Homework_CircleMove0314
理论:不可在分线程中刷新UI,否则无法实现预期界面效果。
源码:
- (void)viewDidLoad
{
[super viewDidLoad];
//1.在圆心添加太阳
UIImageView *sun = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"fireball"]];
sun.center = CGPointMake(CENTER_X, CENTER_Y);
sun.bounds = CGRectMake(0, 0, 60, 60);
[self.view addSubview:sun];
//2.在圆周上添加地球
UIImageView *earth = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"a"]];
earth.center = CGPointMake(CENTER_X+RADIOUS, CENTER_Y);
earth.bounds = CGRectMake(0, 0, 60, 60);
[self.view addSubview:earth];
//3.开启分线程
[NSThread detachNewThreadSelector:@selector(newThread:) toTarget:self withObject:earth];
}
//分线程
//处理耗时数据
- (void)newThread:(UIImageView *)earth
{
//无限循环
for (; ; )
{
[NSThread sleepForTimeInterval:0.01];
//根据角度,计算x,y坐标
static float angle = 0;
angle += 1;
float huDu = angle / 180 * M_PI;
_x = CENTER_X + RADIOUS * cos(huDu);
_y = CENTER_Y + RADIOUS * sin(huDu);
[self performSelectorOnMainThread:@selector(refreshUI:) withObject:earth waitUntilDone:NO];
}
}
//主线程
//刷新UI
- (void)refreshUI:(UIImageView *)earth
{
earth.center = CGPointMake(_x, _y);
}
二、UINavigationController
项目:UINavigationController0315
理论:
栈:后进先出 队列:先进先出
导航(NavController):继承于UIViewController,根据栈的原理,实现多个视图的之间的切换功能。
界面跳转方法共4种:①root②模态弹出③
④UINavigationController:
步骤:
1.创建ViewController
2.创建NavigationController,且设置vc为nav的root
3.将nav设置为window的root
进栈:pushViewController: animated:
出栈:pop
方法1:(到上一vc)popViewController
方法2:(到root)popToRootViewController
方法3:(到任意vc)popToViewController
源码:
文件:AppDelegate.m
//1.创建视图控制器
ViewController1 *vc1 = [[ViewController1 alloc]init];
//2.创建导航控制器,设置vc1为导航的根视图控制器
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc1];
//3.将nav作为window的根视图控制器
self.window.rootViewController = nav;
文件:ViewController1.m
//下一页
//从vc1跳转到vc2,实现压栈(push/视图换入)
- (IBAction)nextClick:(UIButton *)sender
{
//1.创建vc2
ViewController2 *vc2 = [[ViewController2 alloc]init];
//2.压栈,push
[self.navigationController pushViewController:vc2 animated:YES];
}
文件:ViewController2.m
//从vc2跳转到vc1,实现视图的换出(pop/出栈)
//由3种方式
- (IBAction)backClick:(UIButton *)sender
{
//pop
//方法1:直接返回到上一级界面
[self.navigationController popViewControllerAnimated:YES];
//方法2:返回到根视图控制器
[self.navigationController popToRootViewControllerAnimated:YES];
//方法3:
[self.navigationController popToViewController: animated:YES];
}
//跳转到vc3
- (IBAction)nextClick:(UIButton *)sender
{
ViewController3 *vc3 = [[ViewController3 alloc]init];
[self.navigationController pushViewController:vc3 animated:YES];
}
文件:ViewController4.m
//vc4到vc2
- (IBAction)backClick:(UIButton *)sender
{
//第三种pop
//先找到vc2
//1.获取数组
NSArray *vcArr = [self.navigationController viewControllers];
//2.获取vc2
ViewController2 *vc2 = [vcArr objectAtIndex:1];
//3.popTo vc2
[self.navigationController popToViewController:vc2 animated:YES];
}
三、Navigation_Title_Button
项目:Navigation_Title_Button0315
理论:
1.显示导航栏标题
1.1 方法1:self.title
1.2 方法2:self.navigationItem.title
2.添加导航按钮
2.1 系统提供的方式
1>initWithBarButtonSystemItem: target: action:
2>initWithTitle: style: target: action:
3>initWithImage: style: target: action:
2.2 自定义方式
步骤:
1.创建button,设置其bounds/action等
2.声明UIBarButtonItem,并initWithCustomView:button
3.将button添加到nav
源码:
//1.显示导航标题
//方法1:
//title:Localized title for use by a parent controller
self.title = @"主题";
//方法2:
//navigationItem:
//The navigation item used to represent the view controller in a parent’��s navigation bar. (read-only)
self.navigationItem.title = @"设置";
//2.添加导航按钮
//2.1 系统提供的几种方式
//(1)系统图形按钮
UIBarButtonItem *item1 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(photoClick:)];
//self.navigationItem.rightBarButtonItem = item1;
//(2)文字按钮
UIBarButtonItem *item2 = [[UIBarButtonItem alloc]initWithTitle:@"删除" style:UIBarButtonItemStylePlain target:self action:@selector(deleteClick)];
// self.navigationItem.leftBarButtonItem = item2;
//(3)图片按钮
//创建图片
UIImage *image = [UIImage imageNamed:@"scratch"];
//设置图片渲染模式
image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//使用渲染过后的图片
UIBarButtonItem *item3 = [[UIBarButtonItem alloc]initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(animationButton)];
//创建item数组
NSArray *itemArr = [[NSArray alloc]initWithObjects:item1,item3, nil];
//将item数组赋给nav
self.navigationItem.rightBarButtonItems = itemArr;
//2.2 自定义
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//注意:位置坐标不起作用
// button.center = CGPointMake(100, 30);
button.bounds = CGRectMake(0, 0, 40, 40);
button.backgroundColor = [UIColor redColor];
[button addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *item4 = [[UIBarButtonItem alloc]initWithCustomView:button];
NSArray *itemArr2 = [[NSArray alloc]initWithObjects:item2,item4, nil];
self.navigationItem.leftBarButtonItems = itemArr2;
四、隐藏导航栏
项目:NavigationBar0315
理论:
设置导航隐藏:self.navigationController.navigationBarHidden = YES;
位置:一般设置在viewWillAppear:
源码:
//在将要显示时,设置Hidden
//而不是在viewDidLoad设置
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.navigationController.navigationBarHidden = YES;
}
五、使用storyBoard创建导航栏
项目:Navigation_UseStoryBoard0315
理论:
1.选中storyboard
2.点击Editor→Embed In→Navigation Controller