Objective-C 学习笔记 09 - 第一个iOS应用程序
2015-01-31 11:27
260 查看
这一节我们创建一个名为iTahDoodle的iOS应用程序,这是一个简单的任务管理程序,通过property list文件保存数据。
创建iTahDoodle
新建Empty iOS应用程序工程。如下图所示通过storyboard创建主界面。
AppDelegate.h,声明辅助函数,声明用于存放task的数组,添加task函数和三个UI控件(text field,insert button和table view),最后声明自己遵循<UITableViewDataSource>协议。
修改AppDelegate.m
打开辅助编辑器,添加text field,button和tableView的outlet
在viewDidLoad方法中读取plist并刷新table view
实现addTask方法,读取text field中的字符串添加到tasks数组中,并写入plist文件
程序执行效果如下:
在使用辅助编辑器生成outlet过程中遇到了这样的警告Autosynthesized property 'taskField' will use synthesized instance variable '_taskField', not existing instance variable 'taskField'.
使用辅助编辑器生成outlet后,不需要再手动声明这些变量。只是注意在引用这些变量时,要使用self或者用_taskField方式来引用它们。Apple推荐的是使用self方式来引用。
创建iTahDoodle
新建Empty iOS应用程序工程。如下图所示通过storyboard创建主界面。
AppDelegate.h,声明辅助函数,声明用于存放task的数组,添加task函数和三个UI控件(text field,insert button和table view),最后声明自己遵循<UITableViewDataSource>协议。
#import <UIKit/UIKit.h> // 声明辅助函数,用于返回特定文件的路径,该文件用于保存用户的任务列表信息 NSString *docPath(void); @interface ViewController : UIViewController <UITableViewDataSource> { NSMutableArray *tasks; } - (void)addTask:(id)sender; @end
修改AppDelegate.m
打开辅助编辑器,添加text field,button和tableView的outlet
@interface ViewController () @property (weak, nonatomic) IBOutlet UITextField *taskField; @property (weak, nonatomic) IBOutlet UIButton *insertButton; @property (weak, nonatomic) IBOutlet UITableView *taskTable; @end实现辅助函数docPath
NSString *docPath() { NSArray *pathList = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); return [[pathList objectAtIndex:0] stringByAppendingPathComponent:@"data.td"]; }实现UITableViewDataSource协议的必需方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [tasks count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *c = [[self taskTable] dequeueReusableCellWithIdentifier:@"Cell"]; if (!c) { c = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"]; } NSString *item = [tasks objectAtIndex:[indexPath row]]; [[c textLabel] setText:item]; return c; }
在viewDidLoad方法中读取plist并刷新table view
- (void)viewDidLoad { [super viewDidLoad]; NSArray *plist = [NSArray arrayWithContentsOfFile:docPath()]; if (plist) { tasks = [plist mutableCopy]; } else { tasks = [[NSMutableArray alloc] init]; } if ([tasks count] == 0) { [tasks addObject:@"Walk the dogs"]; [tasks addObject:@"Feed the hogs"]; [tasks addObject:@"Chop the logs"]; } [[self taskTable] setDataSource:self]; [[self taskTable] reloadData]; }
实现addTask方法,读取text field中的字符串添加到tasks数组中,并写入plist文件
- (void)addTask:(id)sender { NSString *t = [[self taskField] text]; if ([t isEqualToString:@""]) { return; } [tasks addObject:t]; [[self taskTable] reloadData]; [[self taskField] setText:@""]; [[self taskField] resignFirstResponder]; [tasks writeToFile:docPath() atomically:YES]; }
程序执行效果如下:
在使用辅助编辑器生成outlet过程中遇到了这样的警告Autosynthesized property 'taskField' will use synthesized instance variable '_taskField', not existing instance variable 'taskField'.
使用辅助编辑器生成outlet后,不需要再手动声明这些变量。只是注意在引用这些变量时,要使用self或者用_taskField方式来引用它们。Apple推荐的是使用self方式来引用。
相关文章推荐
- ios学习笔记(二)第一个应用程序--Hello World
- Objective-C学习笔记一:第一个应用程序
- ios学习笔记(二)第一个应用程序--Hello World
- ios学习笔记(二)第一个应用程序--Hello World
- ios学习笔记(二)第一个应用程序--Hello World
- 黑马程序员——Objective-C程序设计(第4版)学习笔记之09-多态、动态类型和动态绑定——黑马 IOS 技术博客
- IOS学习笔记(一)之安装开发环境与工具与第一个IOS应用程序
- ios学习笔记(二)第一个应用程序--Hello World
- 黑马程序员——Objective-C程序设计(第4版)学习笔记之21-编写iOS应用程序——黑马 IOS 技术博客
- ios学习笔记(二)第一个应用程序--Hello World
- 第二章、Objective-C中的@property、@synthesize和dynamic的用法(iOS学习笔记,从零开始)
- IOS之Objective-C学习笔记(一)
- 【Adobe AIR学习笔记】创建第一个Flex AIR应用程序在MyEclipse中
- iOS学习笔记04—Objective-C property和instance variable
- 第二章、Objective-c 语法,类/属性/函数(iOS学习笔记,从零开始。)
- 03.IOS学习笔记之第一个C语言程序
- IOS学习笔记——Objective-c基础(一)
- IOS之Objective-C学习笔记(五)
- IOS之Objective-C学习笔记(三)
- JSF学习笔记--JSF简介,JSF第一个应用程序