导航器的基本运用
1:创建工程。值得的注意的是选Empty Application。
2:创建两个 UIViewController的两个子类first 和second。
3 添加你的应用所要用到的图片和声音等文件
在appdelegat.m中写入,记得加入相应的头文件
// 第一个页面,第一个视图控制器
first *fst=[[first alloc] init];
UINavigationController *n=[[UINavigationController alloc] initWithRootViewController:fst];
//显示第一个页面,并且将该页面的view铺满整个window
self.window.rootViewController=n;
在first.m中写入
#import "first.h"
#import "second.h"
#import<AudioToolbox/AudioToolbox.h>
@interface first ()
{
UIImageView *ff,*gg;
}
@end
@implementation first
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
//创建 按钮
-(void)createbutton1
{
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
btn.frame=CGRectMake(60, 43,30, 30);
[btn setImage:[UIImage imageNamed:@"a0017"] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(didbtn:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
UIButton *btn2=[UIButton buttonWithType:UIButtonTypeCustom];
btn2.frame=CGRectMake(115,43, 30, 30);
[btn2 setImage:[UIImage imageNamed:@"i0009"] forState:UIControlStateNormal];
[btn2 addTarget:self action:@selector(didbtn2:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn2];
}
//按钮一的方法:
-(void)didbtn:(UIButton *)sender
{
second *scd=[[second alloc] init];
UIImageView *sk=[[UIImageView alloc] init];
scd.strok=sk;
scd.name = 1;
scd.strok.image=[UIImage imageNamed:@"a0001"];
//将一个新的页面在导航栈中显示出来, 入栈、压栈
//所有已经在导航控制器管理下的页面(UIViewController)都可以通过一个navigationController的属性访问它所在的导航控制器
[self.navigationController pushViewController:scd animated:YES];
}
//按钮二的方法:
-(void)didbtn2:(UIButton *)sender
{
second *scd=[[second alloc] init];
_strok =[[UIImageView alloc] init];
scd.strok=_strok;
scd.name = 2;
scd.strok.image=[UIImage imageNamed:@"i0001"];
[self.navigationController pushViewController:scd animated:YES];
}
//创建的视图
-(void)createview1
{
ff=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
ff.image=[UIImage imageNamed:@"bg_01"];
[self.view addSubview:ff];
gg=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
gg.image=[UIImage imageNamed:@"bg_01_01"];
[self.view addSubview:gg];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self createview1];
[self createbutton1];
self.navigationController.navigationBarHidden=YES;
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose
of any resources that can be recreated.
}
在second.h中写入
@interface second : UIViewController
@property int name;
@property UIImageView *strok;
@end
在second.m中写入:
#import "first.h"
#import "second.h"
#import<AudioToolbox/AudioToolbox.h>
@interface second ()
{
//定义全局变量
UIImageView *bottomBg,*titleView,*kanaDeta,*stk;
SystemSoundID sound1;
}
@end
@implementation second
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
//创建 视图
-(void)creatview2
{
//中间图片
kanaDeta=[[UIImageView alloc] initWithFrame:CGRectMake(0, 44, 320, 418)];
kanaDeta.image=[UIImage imageNamed:@"kanaDeta"];
[self.view addSubview:kanaDeta];
//顶部图片
titleView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 49)];
titleView.image=[UIImage imageNamed:@"titleView"];
[self.view addSubview:titleView];
//底部图片
bottomBg=[[UIImageView alloc ] initWithFrame:CGRectMake(0, self.view.frame.size.height-49, 320, 49)];
bottomBg.image=[UIImage imageNamed:@"bottomBg"];
[self.view addSubview:bottomBg];
}
//创建 按键
-(void)creatbutton2
{
//返回按钮
UIButton *backBtn=[UIButton buttonWithType:UIButtonTypeCustom];
//把按钮放置图片
[backBtn setImage:[UIImage imageNamed:@"backBtn"] forState:UIControlStateNormal];
backBtn.frame=CGRectMake(10, 6, 70, 35);
[backBtn addTarget:self action:@selector(didback:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:backBtn];
//清除按钮
UIButton *clearbtn=[UIButton buttonWithType:UIButtonTypeCustom];
[clearbtn setImage:[UIImage imageNamed:@"clearBtn"] forState:UIControlStateNormal];
clearbtn.frame=CGRectMake(self.view.frame.size.width-80, 6, 70, 35);
[clearbtn addTarget:self action:@selector(didclear:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:clearbtn];
//练习按钮
UIButton *practicebtn=[UIButton buttonWithType:UIButtonTypeCustom];
practicebtn.frame=CGRectMake(245, self.view.frame.size.height-52, 67, 52);
[practicebtn setImage:[UIImage imageNamed:@"practiceBtn_01"] forState:UIControlStateNormal];
[practicebtn setImage:[UIImage imageNamed:@"practiceBtn_02"] forState:UIControlStateHighlighted];
[practicebtn addTarget:self action:@selector(didpractice:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:practicebtn];
//发音
UIButton *pronounceBtn=[UIButton buttonWithType:UIButtonTypeCustom];
pronounceBtn.frame=CGRectMake(10, self.view.frame.size.height - 52, 67, 52);
[pronounceBtn setImage:[UIImage imageNamed:@"pronounceBtn_01"] forState:UIControlStateNormal];
[pronounceBtn setImage:[UIImage imageNamed:@"pronounceBtn_02"] forState:UIControlStateHighlighted];
[pronounceBtn addTarget:self action:@selector(didpronounce:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:pronounceBtn];
//笔顺
UIButton *stroke=[UIButton buttonWithType:UIButtonTypeCustom];
stroke.frame=CGRectMake(125, self.view.frame.size.height-52, 67, 52);
[stroke setImage:[UIImage imageNamed:@"strokeOrderBtn_01"] forState:UIControlStateNormal];
[stroke setImage:[UIImage imageNamed:@"strokeOrderBtn_02"] forState:UIControlStateHighlighted];
[stroke addTarget:self action:@selector(didstroke:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:stroke];
}
//练习按键
-(void)didpractice:(UIButton *)sender
{
_strok.frame=CGRectMake(50, 100, 225, 225);
[self.view addSubview:_strok];
NSLog(@"%s",__PRETTY_FUNCTION__);
}
//笔顺按键
-(void)didstroke:(UIButton *)sender
{
stk=[[UIImageView alloc]initWithFrame:CGRectMake(50, 100, 225, 225)];
NSMutableArray *shu=[[NSMutableArray alloc] init];
//判断来自于哪个按键,并给相应的笔顺。
if(self.name==1)
{
for(int i=1;i<=17;i++)
{
NSString *name=[NSString stringWithFormat:@"a%04d",i];
UIImage *image=[UIImage imageNamed:name];
[shu addObject: image];
}
stk.image=[UIImage imageNamed:@"a0017"];
}
else if(self.name==2)
{
for(int i=1;i<=9;i++)
{
NSString *name=[NSString stringWithFormat:@"i%04d",i];
UIImage *image=[UIImage imageNamed:name];
[shu addObject: image];
}
//循环结束时显示显示的图片
stk.image=[UIImage imageNamed:@"i0009"];
}
//循环时间
stk.animationDuration=2;
//次数
stk.animationRepeatCount=1;
stk.animationImages=shu;
[stk startAnimating];
[self.view addSubview:stk];
NSLog(@"%s",__PRETTY_FUNCTION__);
}
//返回按键
-(void)didback:(UIButton *)sender
{
//返回到导航控制器的上一个页面
[self.navigationController popViewControllerAnimated:YES];
NSLog(@"%s",__PRETTY_FUNCTION__);
}
//清除按键
-(void)didclear:(UIButton *)sender
{
[_strok removeFromSuperview];
// self.strok = nil;
[stk removeFromSuperview];
NSLog(@"%s",__PRETTY_FUNCTION__);
}
//发音按键
-(void)didpronounce:(UIButton *)sender
{
NSLog(@"%s",__PRETTY_FUNCTION__);
AudioServicesPlaySystemSound(sound1);
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self creatview2];
[self creatbutton2];
//判断来自于哪个按键。按键不同,所给的声音也不同
if(self.name==1)
{
NSURL *url=[[NSBundle mainBundle] URLForResource:@"000_a" withExtension:@"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)(url),&sound1);
}
else if(self.name==2)
{
NSURL *url=[[NSBundle mainBundle] URLForResource:@"001_i" withExtension:@"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)(url),&sound1);
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
运行后的结果为: