创建项目
需要把Use Core Date勾上
把Main.storyboard删除.
点击如图文件.
点击下面的Add Entity
点击 + 添加属性和设置类型
选中Entity,修改为Person ,再点击editer->Create....->一直下一步.
新建了两个文件.
创建window
AppDelegate::- (BOOL)application: didFinishLaunchingWithOptions:- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {运行一下,白白的.
// 创建WINDOW
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// 设置为白色
[self.window setBackgroundColor:[UIColor whiteColor]];
// 设置主窗口(应该是这个意思)<img alt="疑问" src="http://static.blog.csdn.net/xheditor/xheditor_emot/default/doubt.gif" />
[self.window makeKeyAndVisible];
return YES;
}
设计视图
在ViewController.h中添加属性.@interface ViewController : UIViewController
@property (nonatomic, retain) UIButton *buttonOfInsert;
@property (nonatomic, retain) UIButton *buttonOfGetAll;
@property (nonatomic, retain) UIButton *buttonOfDelete;
@property (nonatomic, retain) UIButton *buttonOfUpdate;
@property (nonatomic, retain) UIButton *buttonOfFind;
@property (nonatomic, retain) UITextField *textFieldOfPid;
@property (nonatomic, retain) UITextField *textFieldOfPname;
@property (nonatomic, retain) UITextField *textFieldOfAddress;
@property (nonatomic, retain) UILabel *labelOfPid;
@property (nonatomic, retain) UILabel *labelOfPname;
@property (nonatomic, retain) UILabel *labelOfAddress;
@end
在ViewController.m中新增消息.
#pragma mark 界面设计
/*添加按钮*/
- (void) setButtons{
CGRect screen = [[UIScreen mainScreen] bounds]; // 获取全屏尺寸
CGFloat width = screen.size.width / 6.0; // 每个按钮长
CGFloat residue = screen.size.width - width * 5; // 剩余空间
CGFloat dis = residue / 6; // 每个按钮的空隙
CGFloat y = (screen.size.height * 4)/8.0; // y轴位置
CGFloat height = screen.size.height / 10.0; // 高
self.buttonOfInsert = [self buildButtonWithTitle:@"INSERT" andFrame:CGRectMake(dis, y, width, height)];
self.buttonOfGetAll = [self buildButtonWithTitle:@"ALL" andFrame:CGRectMake(dis * 2 + width, y, width, height)];
self.buttonOfFind = [self buildButtonWithTitle:@"FIND" andFrame:CGRectMake(dis * 3 + width * 2, y, width, height)];
self.buttonOfDelete = [self buildButtonWithTitle:@"DELETE" andFrame:CGRectMake(dis * 4 + width * 3, y, width, height)];
self.buttonOfUpdate = [self buildButtonWithTitle:@"UPDATE" andFrame:CGRectMake(dis * 5 + width * 4, y, width, height)];
}
/*添加输入框*/
- (void) setTextFields{
CGRect screen = [[UIScreen mainScreen] bounds]; // 获取全屏尺寸
CGFloat width = screen.size.width * 2 / 3.0 - 20; // 每个输入框长
CGFloat dis = 10; // 每个输入框的空隙
CGFloat y = (screen.size.height * 2)/8.0; // y轴位置
CGFloat height = screen.size.height / 18.0; // 高
self.textFieldOfPid = [self buildTextFieldWithFrame:CGRectMake(width / 2.0, y, width, height)];
self.textFieldOfPname = [self buildTextFieldWithFrame:CGRectMake(width / 2.0, y + height + dis, width, height)];
self.textFieldOfAddress = [self buildTextFieldWithFrame:CGRectMake(width / 2.0 , y + height * 2 + dis * 2, width, height)];
}
/*添加文本*/
- (void)setLabels{
CGRect screen = [[UIScreen mainScreen] bounds]; // 获取全屏尺寸
CGFloat width = screen.size.width * 1 / 3.0 - 40; // 每个文本长
CGFloat dis = 10; // 每个文本的空隙
CGFloat y = (screen.size.height * 2)/8.0; // y轴位置
CGFloat height = screen.size.height / 18.0; // 高
self.labelOfPid = [self buildLabelWithTitle:@"PID" andFrame:CGRectMake(20, y, width, height)];
self.labelOfPname = [self buildLabelWithTitle:@"PNAME" andFrame:CGRectMake(20, y + height + dis, width, height)];
self.labelOfAddress = [self buildLabelWithTitle:@"ADDRESS" andFrame:CGRectMake(20 , y + height * 2 + dis * 2, width, height)];
}
/*获取到一个文本*/
- (UILabel *)buildLabelWithTitle:(NSString *) title andFrame:(CGRect) frame{
UILabel *labelOfTemp = [[UILabel alloc] initWithFrame:frame];
[labelOfTemp setTintColor:[UIColor blackColor]];
labelOfTemp.text = title;
// 向右对齐
[labelOfTemp setTextAlignment:NSTextAlignmentRight];
[self.view addSubview:labelOfTemp];
return labelOfTemp;
}
/*获取一个输入框*/
- (UITextField *) buildTextFieldWithFrame:(CGRect) frame{
UITextField *textFieldOfTemp = [[UITextField alloc] initWithFrame:frame];
[textFieldOfTemp setBorderStyle:UITextBorderStyleRoundedRect]; // 设置框的形式
[self.view addSubview:textFieldOfTemp];
return textFieldOfTemp;
}
/* 获取一个按钮 */
- (UIButton *) buildButtonWithTitle:(NSString *) title andFrame:(CGRect) frame{
UIButton *buttonOfTemp = [UIButton buttonWithType:UIButtonTypeSystem];
buttonOfTemp.frame =frame;
[buttonOfTemp setTitle:title forState:UIControlStateNormal];
[buttonOfTemp setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[buttonOfTemp.titleLabel setFont:[UIFont systemFontOfSize:14.0]]; // 设置字体大小
[self.view addSubview:buttonOfTemp];
return buttonOfTemp;
}
在- (void)viewDidLoad加上:
[self setButtons];回到 AppDelegate.m 在- (BOOL)application: didFinishLaunchingWithOptions:添加如下代码
[self setTextFields];
[self setLabels];
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {来运行 一下:
// 创建WINDOW
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// 设置为白色
[self.window setBackgroundColor:[UIColor whiteColor]];
// 创建一个ViewController
ViewController *vc = [[ViewController alloc] init];
// 设置为window的根视图控制器
[self.window setRootViewController:vc];
// 设置主窗口(应该是这个意思)
[self.window makeKeyAndVisible];
return YES;
}
获取上下文 NSManagedObjectContext
导入包:#import "AppDelegate.h"在ViewController.m中添加消息:
#pragma mark Core Date
/*获取上下文,增删改查都需要用到这个,*/
- (NSManagedObjectContext *)managedObjectContext{
AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
if ([delegate performSelector:@selector(managedObjectContext)]) {
return [delegate managedObjectContext];
}else{
return nil;
}
}
添加按钮的动作
在ViewController.m中添加消息:
#pragma mark 创建按钮动作
- (void)actinOfButtons{
[self.buttonOfInsert addTarget:self action:@selector(insert) forControlEvents:UIControlEventTouchUpInside];
[self.buttonOfGetAll addTarget:self action:@selector(getall) forControlEvents:UIControlEventTouchUpInside];
[self.buttonOfDelete addTarget:self action:@selector(delete) forControlEvents:UIControlEventTouchUpInside];
[self.buttonOfFind addTarget:self action:@selector(find) forControlEvents:UIControlEventTouchUpInside];
[self.buttonOfUpdate addTarget:self action:@selector(update) forControlEvents:UIControlEventTouchUpInside];
}
// 删除数据
- (void) delete{
}
// 查找数据
- (void) find{
}
// 更行数据
- (void) update{
}
// 保存数据
- (void) insert{
}
// 获取所有数据
- (void) getall{
}
在 - ( void )viewDidLoad加上:
[self actinOfButtons];
导入包
#import "Person.h"
Core Date
实现保存和获取所有数据:
//保存数据操作
- (void) insert{
NSString *pid = self.textFieldOfPid.text;
NSString *pname = self.textFieldOfPname.text;
NSString *address = self.textFieldOfAddress.text;
// 获取上下文对象
NSManagedObjectContext *cxt = [self managedObjectContext];
// 插入一个新对象
NSManagedObjectContext *person = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:cxt];
// 给person赋值
[person setValue:pid forKey:@"pid"];
[person setValue:pname forKey:@"pname"];
[person setValue:address forKey:@"address"];
// 创建一个错误对象,在错误时保存错误信息
NSError *error = nil;
if ([cxt save:&error]) {
NSLog(@"add success");
}else{
NSLog(@"add fail");
// 打印错误信息
NSLog(@"Reason:%@",error);
}
}
- (void) getall{
NSManagedObjectContext *cxt = [self managedObjectContext];
// 抓取数据,抓取Person表
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Person"];
// cxt 执行:
NSArray *personArr = [cxt executeFetchRequest:request error:nil];
if (personArr != nil && personArr.count > 0) {
for (Person *obj in personArr) {
NSLog(@"%@\t%@\t%@\t",obj.pid, obj.pname, obj.address);
}
}
}
运行一下
其他的依法炮制:
// 删除数据补充,排序.
- (void) delete{
// 根据pid删除
NSString *pid = self.textFieldOfPid.text;
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Person"];
NSManagedObjectContext *cxt = [self managedObjectContext];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"pid = %@",pid];
[request setPredicate:predicate];
NSArray *personArr = [cxt executeFetchRequest:request error:nil];
if (personArr != nil && personArr.count > 0) {
Person *person = [personArr lastObject];
[cxt deleteObject:person];
// 相当于提交
[cxt save:nil];
NSLog(@"del success");
}
}
// 查找数据
- (void) find{
// 根据pid查询
NSString *pid = self.textFieldOfPid.text;
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Person"];
// 创建一个筛选
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"pid = %@",pid];
[request setPredicate:predicate];
NSManagedObjectContext *cxt = [self managedObjectContext];
NSArray *personArr = [cxt executeFetchRequest:request error:nil];
if (personArr != nil && personArr.count > 0) {
for (Person *obj in personArr) {
NSLog(@"%@\t%@\t%@\t",obj.pid, obj.pname, obj.address);
}
}
}
// 更行数据
- (void) update{
// 获取pid
NSString *pid = self.textFieldOfPid.text;
NSManagedObjectContext *cxt = [self managedObjectContext];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"pid = %@", pid];
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Person"];
[request setPredicate:predicate];
NSArray *personArr = [cxt executeFetchRequest:request error:nil];
if (personArr != nil && personArr.count > 0) {
Person *person = [personArr lastObject];
person.pname = self.textFieldOfPname.text;
person.address = self.textFieldOfAddress.text;
// 提交
[cxt save:nil];
NSLog(@"updata success");
}
}
- (void)sort{
// 根据pid排序,降序
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Person"];
// 查询规则
NSSortDescriptor *pidDesc = [[NSSortDescriptor alloc] initWithKey:@"pid"
ascending:NO // 是否升序,NO
];
[request setSortDescriptors:@[pidDesc]];
NSManagedObjectContext *cxt = [self managedObjectContext];
NSArray *personArr = [cxt executeFetchRequest:request error:nil];
if (personArr != nil && personArr.count > 0) {
for (Person *obj in personArr) {
NSLog(@"%@\t%@\t%@",obj.pid, obj.pname, obj.address);
}
}
}
完全真实学习&&参考--->点击打开链接