|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
所以你可以用很多方法存储数据比如最长用的sqlite当然如果另类也可以用plist文件或者其他NSManagedObjectContextNSManagedObjectContext比来事情不忙,空闲之余自学了一下object-c,体验了一把ios无敌的framework。想写个小使用练练手,给本人挑了个复杂的标题:仿照完成一下ios体系使用时钟里的秒表程序,就是这个使用:
<br>
次要完成的功效:
1.由start/stop键完成计时
2.有reset/lap键完成复位和计次
必要思索的点:
1.工夫的暗示办法(有良多种思绪)
2.计次数据的倒序分列,即计次1的数据在最底端,顺次向上为计次2,计次3的工夫数据
我的完成:
ARC省往了我们自行办理内存的年夜部分事变,写惯了c++因而恬逸了良多
[cpp]
- -(IBAction)startOrstop:(UIButton*)sender
- {
- //点击切换按钮背景图
- UIImage*newImage=(checked)?[UIImageimageNamed:@"red.png"]:[UIImageimageNamed:@"green.png"];
- [leftBtnsetBackgroundImage:newImageforState:UIControlStateNormal];
- NSString*titlel=(checked)?(@"Stop"):(@"Start");
- [leftBtnsetTitle:titlelforState:UIControlStateNormal];
- NSString*titler=(checked)?(@"Lap"):(@"Reset");
- [rightBtnsetTitle:titlerforState:UIControlStateNormal];
- if(checked)//start
- {
- timer=[NSTimerscheduledTimerWithTimeInterval:0.1target:selfselector:@selector(updateTime)userInfo:nilrepeats:YES];
- }else{//stop
- [timerinvalidate];
- }
- checked=!checked;
- }
- -(IBAction)resetOrLap:(UIButton*)sender
- {
- staticNSIntegercount=1;
- if(checked)//reset
- {
- time=time_lap=0.0;
- timestr=[NSStringstringWithFormat:@"00:00.0"];
- [labelsetText:timestr];
- list_time=list_lap=nil;
- count=1;
- [tableviewreloadData];
- }else{//lap
- if(list_time==nil){
- list_time=[[NSArrayalloc]initWithObjects:timestr_lap,nil];
- list_lap=[[NSArrayalloc]initWithObjects:[NSStringstringWithFormat:@"%d",count++],nil];
- }else{
- #if0
- [listarrayByAddingObject:timestr];
- #else
- NSArray*array=[[NSArrayalloc]initWithObjects:timestr_lap,nil];
- list_time=[arrayarrayByAddingObjectsFromArray:list_time];
- array=[[NSArrayalloc]initWithObjects:[NSStringstringWithFormat:@"%d",count++],nil];
- list_lap=[arrayarrayByAddingObjectsFromArray:list_lap];
- #endif
- }
- time_lap=0;
- [tableviewreloadData];
- }
- }
- -(float)updateTime
- {
- time+=0.1;
- time_lap+=0.1;
- timestr=[NSStringstringWithFormat:@"%02d:%04.1f",(int)(time/60),time-(60*(int)(time/60))];
- timestr_lap=[NSStringstringWithFormat:@"%02d:%04.1f",(int)(time_lap/60),time_lap-(60*(int)(time_lap/60))];
- [labelsetText:timestr];
- returntime;
- }
- -(NSInteger)tableView:(UITableView*)tableViewnumberOfRowsInSection:(NSInteger)section
- {
- return[list_timecount];
- }
- -(UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath*)indexPath
- {
- staticNSString*tableViewIdentifier=@"tableViewIdentifier";
- UITableViewCell*cell=[tableViewdequeueReusableCellWithIdentifier:tableViewIdentifier];
- if(cell==nil){
- cell=[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleValue1reuseIdentifier:tableViewIdentifier];
- }
- NSUIntegerrow=[indexPathrow];
- cell.detailTextLabel.text=[list_timeobjectAtIndex:row];
- cell.detailTextLabel.textColor=[UIColorblackColor];
- cell.detailTextLabel.font=[UIFontboldSystemFontOfSize:25.0];
- cell.detailTextLabel.textAlignment=UITextAlignmentCenter;
- NSString*text=[[NSStringalloc]initWithFormat:@"lap%@",[list_lapobjectAtIndex:row]];
- cell.textLabel.text=text;
- returncell;
- }
-(IBAction)startOrstop:(UIButton*)sender{//点击切换按钮背景图UIImage*newImage=(checked)?[UIImageimageNamed:@"red.png"]:[UIImageimageNamed:@"green.png"];[leftBtnsetBackgroundImage:newImageforState:UIControlStateNormal];NSString*titlel=(checked)?(@"Stop"):(@"Start");[leftBtnsetTitle:titlelforState:UIControlStateNormal];NSString*titler=(checked)?(@"Lap"):(@"Reset");[rightBtnsetTitle:titlerforState:UIControlStateNormal];if(checked)//start{timer=[NSTimerscheduledTimerWithTimeInterval:0.1target:selfselector:@selector(updateTime)userInfo:nilrepeats:YES];}else{//stop[timerinvalidate];}checked=!checked;}-(IBAction)resetOrLap:(UIButton*)sender{staticNSIntegercount=1;if(checked)//reset{time=time_lap=0.0;timestr=[NSStringstringWithFormat:@"00:00.0"];[labelsetText:timestr];list_time=list_lap=nil;count=1;[tableviewreloadData];}else{//lapif(list_time==nil){list_time=[[NSArrayalloc]initWithObjects:timestr_lap,nil];list_lap=[[NSArrayalloc]initWithObjects:[NSStringstringWithFormat:@"%d",count++],nil];}else{#if0[listarrayByAddingObject:timestr];#elseNSArray*array=[[NSArrayalloc]initWithObjects:timestr_lap,nil];list_time=[arrayarrayByAddingObjectsFromArray:list_time];array=[[NSArrayalloc]initWithObjects:[NSStringstringWithFormat:@"%d",count++],nil];list_lap=[arrayarrayByAddingObjectsFromArray:list_lap];#endif}time_lap=0;[tableviewreloadData];}}-(float)updateTime{time+=0.1;time_lap+=0.1;timestr=[NSStringstringWithFormat:@"%02d:%04.1f",(int)(time/60),time-(60*(int)(time/60))];timestr_lap=[NSStringstringWithFormat:@"%02d:%04.1f",(int)(time_lap/60),time_lap-(60*(int)(time_lap/60))];[labelsetText:timestr];returntime;}-(NSInteger)tableView:(UITableView*)tableViewnumberOfRowsInSection:(NSInteger)section{return[list_timecount];}-(UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath*)indexPath{staticNSString*tableViewIdentifier=@"tableViewIdentifier";UITableViewCell*cell=[tableViewdequeueReusableCellWithIdentifier:tableViewIdentifier];if(cell==nil){cell=[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleValue1reuseIdentifier:tableViewIdentifier];}NSUIntegerrow=[indexPathrow];cell.detailTextLabel.text=[list_timeobjectAtIndex:row];cell.detailTextLabel.textColor=[UIColorblackColor];cell.detailTextLabel.font=[UIFontboldSystemFontOfSize:25.0];cell.detailTextLabel.textAlignment=UITextAlignmentCenter;NSString*text=[[NSStringalloc]initWithFormat:@"lap%@",[list_lapobjectAtIndex:row]];cell.textLabel.text=text;returncell;}
最终效果:
<br>
<br>
<br>
待改善的中央:
1.关于工夫的计时操纵和UI事务应当分分歧线程完成,这里我偷懒了
2.关于工夫的暗示办法实在也是很偷懒的,没有依照尺度的秒分进位暗示
若有发明任何毛病、不当的中央请指导,感谢。
把上面两个对象连在一起把他们变成一个整体所有的CD操作都是通过这个类的这个需要仔细看文档了举个不恰当的例子就像三个人收拾衣服一个人负责衣服的存放位置(NSManagedObjectModel)一个人负责把衣服分类冬天穿夏天穿等(NSPersistentStoreCoordinator) |
|