盲人摸象——IOS简单应用ToDoList(Objective-c)
来源:互联网 发布:火影忍者av软件 编辑:程序博客网 时间:2024/06/05 12:48
学习IOS两个月,看无线互联的视频和一些东拼西凑的博客,基础没有打好,学习的条理不清晰。还是通过东拼西凑的方式做了一个界面很简单的ToDoList应用,用到一些很基础的知识来实现安排计划、标记完成和取消标记、删除这些十分简单的功能(功能简单到还没有中文输入)。
如上图所示,在textfield框中输入你的计划,点击“Go!”,计划就会在下方列表中显示,当点击列表中每行右端的小方格或者选中每一行,就会标记计划,再次点击取消标记,标记标志为方格中的对勾图形。
将已经显示的计划选中向左方滑动,出现删除选项,点击删除。
这么一个简单的东西,而且还缺少了很多必须要完善的功能,我还是折腾了四五天才勉强做出来,让我深刻地体会到“盲人摸象”和“磨刀不误砍柴工”,还是回去把OC基础语法的视频再过一遍。
在这个程序的编写中,我初步(但是还没有完全)形成mvc模式,传输数据在命名为Task的类中定义:
//Task.h#import <Foundation/Foundation.h>@interface Task : NSObject@property (nonatomic, strong) NSString *content;//输入的计划内容@property (nonatomic, assign) BOOL isMark;//标记标志量@property (nonatomic, strong) NSString *date;//计划建立时间@end
//Task.m#import "Task.h"@implementation Task- (id)init{ self = [super init]; if (self) { NSDate *today = [NSDate date]; NSDateFormatter *formate = [[NSDateFormatter alloc]init]; [formate setDateFormat:@"hh:mm"];//取出当前时间// [formate setDateFormat:@"yyyy-MM-dd hh:mm:ss"]; _date = [formate stringFromDate:today]; } return self;}@end
输入需要的控件在inputTask类中定义:
//imputTask.h#import <UIKit/UIKit.h>@interface InputTask : UIView<UITextFieldDelegate>@property (nonatomic, strong) UITextField *inputText;//输入框@property (nonatomic, strong) UIButton *button;//按钮“Go!”@end
#import "RootViewController.h"#import "InputTask.h"@implementation InputTask- (id)initWithFrame:(CGRect)frame{ self = [super initWithFrame:(CGRect)frame]; if (self) { _inputText = [[UITextField alloc] initWithFrame:CGRectMake(60, 30, 200, 35)]; _inputText.delegate = self; // 设置代理 _inputText.textColor = [UIColor greenColor]; _inputText.placeholder = @"input"; _inputText.adjustsFontSizeToFitWidth = YES; _inputText.clearsOnBeginEditing = YES; _inputText.clearButtonMode = UITextFieldViewModeWhileEditing; _inputText.borderStyle = UITextBorderStyleRoundedRect; self.userInteractionEnabled = YES;// [_inputText.window makeKeyAndVisible]; _button = [UIButton buttonWithType:UIButtonTypeCustom]; _button.frame = CGRectMake(265, 30, 50, 35);// [_button addTarget:self action:@selector() forControlEvents:UIControlEventTouchUpInside]; [_button setTitle:@"Go!" forState:UIControlStateNormal]; [_button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; [_button setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted]; [_button sizeToFit]; [self addSubview:_inputText]; [self addSubview:_button]; } return self;}#pragma mark - textField Delegate- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{ NSLog(@"textFieldShouldBeginEditing"); [textField.window makeKeyAndVisible]; return YES; // [textField becomeFirstResponder];}- (void)textFieldDidBeginEditing:(UITextField *)textField{ if (!textField.window.isKeyWindow) { [textField.window makeKeyAndVisible]; } NSLog(@"textFieldDidBeginEditing");}- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{ NSLog(@"textFieldShouldEndEditing"); return YES; // [tf resignFirstResponder];}- (void)textFieldDidEndEditing:(UITextField *)textField{ NSLog(@"textFieldDidEndEditing : %@", textField.text);}- (BOOL)textField:(UITextField *)textFieldshouldChangeCharactersInRange:(NSRange)rangereplacementString:(NSString *)string{ NSLog(@"shouldChangeCharactersInRange : %@", string); NSLog(@"%@", textField.text); return YES;}- (BOOL)textFieldShouldClear:(UITextField *)textField{ NSLog(@"textFieldShouldClear"); return YES;}- (BOOL)textFieldShouldReturn:(UITextField *)textField{ [textField resignFirstResponder]; return YES;}@end
列表在RootViewController中实现:
//RootViewController.h#import <UIKit/UIKit.h>@interface RootViewController : UIViewController <UITableViewDataSource,UITableViewDelegate>@end
#import "RootViewController.h"#import "TaskCell.h"#import "Task.h"#import "InputTask.h"@interface RootViewController (){ UITableView *_tableView; NSMutableArray *_taskList; InputTask *_inputTask;// UITextField *_tf;// UIButton *_button;}@end@implementation RootViewController- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{ self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self;}- (void)loadView{ UIView *view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame]; self.view = view; _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 70, 320, kDeviceHeight-70) style:UITableViewStylePlain]; _tableView.dataSource = self; _tableView.delegate = self; _tableView.rowHeight = 60; _inputTask = [[InputTask alloc] initWithFrame:CGRectMake(0, 0, kDeviceWidth, 65)]; [_inputTask.button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:_inputTask];// [self.view addSubview:_button]; [self.view addSubview:_tableView];}- (void)buttonAction:(id)sender{// NSLog(@"%@", _tf.text); Task *task = [[Task alloc] init];// task.date = @"今天"; task.content = _inputTask.inputText.text;// task.content = _tf.text; [_taskList insertObject:task atIndex:0]; [_tableView reloadData]; _inputTask.inputText.text = @"";}- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. _taskList = [[NSMutableArray alloc] initWithCapacity:20];}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}#pragma mark - TableView Datasource- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 1;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;{ return [_taskList count];}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *cellIdentify = @"cell"; TaskCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify]; Task *task = _taskList[indexPath.row]; if (cell == nil) { cell = [[TaskCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentify]; } cell.markButton.tag = indexPath.row; cell.buttonClick = ^() { task.isMark = !task.isMark; [_tableView reloadData]; }; cell.date.text = task.date; cell.markButton.selected = task.isMark; cell.textLabel.text = task.content; /*// UIButton *btn4 = [UIButton buttonWithType:UIButtonTypeRoundedRect];// btn4.frame = CGRectMake(270.0f, 0.0f, 50.0f, 40.0f);// [btn4 setTitle:@"删除" forState:UIControlStateNormal];// [btn4 addTarget:self action:@selector(test) forControlEvents:UIControlEventTouchUpInside];// [cell addSubview:btn4]; */ return cell;}- (void)test{// UITextField *tf = (UITextField *)[self.window viewWithTag:101]; // 将键盘移除// [tf resignFirstResponder]; NSLog(@"hha");}//返回YES,表示支持单元格的移动-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{ return YES;}//单元格返回的编辑风格,包括删除 添加 和 默认 和不可编辑三种风格-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{ return UITableViewCellEditingStyleDelete;}-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ if (editingStyle==UITableViewCellEditingStyleDelete) { // 获取选中删除行索引值 NSInteger row = [indexPath row]; // 通过获取的索引值删除数组中的值 [_taskList removeObjectAtIndex:row]; // 删除单元格的某一行时,在用动画效果实现删除过程 [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; }}-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cellView = [tableView cellForRowAtIndexPath:indexPath]; if (cellView.accessoryType == UITableViewCellAccessoryNone) {// cellView.accessoryType=UITableViewCellAccessoryCheckmark;// markButton.selected = !markButton.selected; Task *task = _taskList[indexPath.row]; task.isMark = !task.isMark; [tableView reloadData]; } else { cellView.accessoryType = UITableViewCellAccessoryNone; [tableView deselectRowAtIndexPath:indexPath animated:YES]; }}/*#pragma mark - Navigation// In a storyboard-based application, you will often want to do a little preparation before navigation- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller.}*/@end
UITableView 的单元格cell 中包含了标记按钮和显示时间的UILable,在 TaskCell类中实现:
#import <UIKit/UIKit.h>@interface TaskCell : UITableViewCell@property (nonatomic, strong) UIButton *markButton;@property (nonatomic, strong) UILabel *date;@property (nonatomic, copy) void (^buttonClick)();@end
#import "TaskCell.h"@implementation TaskCell- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { _markButton = [UIButton buttonWithType: UIButtonTypeCustom]; _markButton.frame = CGRectMake(270, 0, 50, 50); [_markButton setImage:[UIImage imageNamed:@"checkbox_priority"] forState:UIControlStateNormal]; [_markButton setImage:[UIImage imageNamed:@"checkbox_checked"] forState:UIControlStateSelected]; [_markButton addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:_markButton]; _date = [[UILabel alloc] initWithFrame:CGRectMake(270, 40, 50, 20)]; [self addSubview:_date]; } return self;}- (void)click:(id)sender { if (_buttonClick) { _buttonClick(); }}- (void)awakeFromNib { // Initialization code}- (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state}@end
这个版本还有很多地方没完善,比如中文输入、响应键盘上的return、我没掌握的block语法(响应标记按钮事件)、没掌握http通信与后台传输数据、单元格动画、丑的不堪的界面等等。盲人摸象做事的效率低下,质量还很差,如果没有导师指导,恐怕要更长的时间才能完成。然而,这并没有完成,我会重新做一个新的版本,加上登陆界面,下一次题目就不能叫盲人摸象了,要做到柳暗花明。
0 0
- 盲人摸象——IOS简单应用ToDoList(Objective-c)
- 盲人摸象——iOS简单应用ToDoList之sqlite数据本地化
- iOS Objective—C 字符串应用
- 盲人摸象、指鹿为马——命名空间
- 第一章:iOS应用安全概要——1.1.2 Objective-C和iOS Frameworks
- flask+mongodb实现简单todolist应用
- flask + vue.js 实现简单todolist应用
- IOS-TODOLIST
- Objective-c,iOS应用开发随笔
- iOS应用开发(三)Objective-C
- IOS学习笔记3—Objective C—简单的内存管理
- 黑马程序员——IOS基础---Objective-C第一节课---最简单的OC程序
- iOS基础——Objective-C基础
- ios学习路线—Objective-C(MRC)
- ios学习路线—Objective-C(ARC)
- ios学习路线—Objective-C(autoreleasepool)
- ios学习路线—Objective-C(Category)
- ios学习路线—Objective-C(Protocol)
- JAVA 之"线程"
- 树莓派 shell(ethtool、miitool)判断网口网线是否插好 方法总结
- Java中GUI界面美化帮助类总结
- 循环-08. 二分法求多项式单根
- 内部排序——堆排序初探
- 盲人摸象——IOS简单应用ToDoList(Objective-c)
- appweb官网资料总结1
- Nodejs获取网络数据并生成Excel表格
- 手把手教你:python字符串编码详解
- hihoCoder Magic Box
- ViewPager与PagerAdapter(PagerAdapter工作流程)
- Codeforces Round #304 (Div. 2) D - Soldier and Number Game
- 软件测试书籍列表
- Java中this用法详解