PicList2 App项目笔记(一)…
2014-03-20 17:25
281 查看
原文地址:App项目笔记(一)didFinishLaunchingWithOptions方法">PicList2 App项目笔记(一)didFinishLaunchingWithOptions方法作者:小小男子汉按照网上的一个ios模板教程仿写了一个小项目。对里面的一些代码总结下。有偏差的地方以后更正。
原文出处:http://maybelost.com/2011/12/tutorial-storyboard-app-with-core-data/
一 Delegate 文件中 didFinishLaunchingWithOptions
方法:顾名思义。在app开始运行时会调用里面的方法。官方文档的定义是"Tells
the delegate when the application has launched and may have
additional launch options to
handle."
它和didReceiveLocalNotification
这个方法的区别是,如果程序是在后台要启动至前台就调用didReceiveLocalNotification。如果程序完全退出,先调用didFinishLaunchingWithOptions把程序启动起来。
附带简单代码
- (BOOL)application:(UIApplication
*)application
didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions
{
// 获得类似首选项的用户初始化定义数据的引用
NSUserDefaults *prefs = [NSUserDefaults
standardUserDefaults];
//检查是否之前app运行过
if ([prefs
boolForKey:@"hasRunBefore"] !=
YES)
{
// 设置个标记使此if中字段只在第一次运行时执行
[prefs setBool:YES
forKey:@"hasRunBefore"];
[prefs synchronize];//
把设置的键值对同步
// Add our default user object in Core
Data。
//NSEntityDescription方法。在已经获得了managedObjectContext的前提下可以通
//过NSEntityDescription方法获得我们之前定义的entity。
Users *user =
(Users
*)[NSEntityDescription
insertNewObjectForEntityForName:@"Users"
inManagedObjectContext:self.managedObjectContext];
[user
setUsername:@"admin"];
[user
setPassword:@"password"];
// Commit to core data
NSError *error;
if
(![self.managedObjectContext
save:&error])
NSLog(@"Failed to add default user with
error: %@", [error
domain]);
}
// Pass the
managed object context to the root view controller (the login
view)
//LoginViewController是我们自己写的文件。rootView作为该类的应用,让delegate文件的rootViewController给LoginViewController赋值。再把delegate文件的managedObjectContext赋值给rootView。
使LoginViewController称为程序入口。
LoginViewController *rootView = (LoginViewController
*)self.window.rootViewController;
rootView.managedObjectContext =
self.managedObjectContext;
//加上下面的会导致一片空白
// self.window = [[UIWindow
alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// // Override point for
customization after application launch.
//
self.window.backgroundColor = [UIColor whiteColor];
// [self.window
makeKeyAndVisible];
return
YES;
}
原文出处:http://maybelost.com/2011/12/tutorial-storyboard-app-with-core-data/
一 Delegate 文件中 didFinishLaunchingWithOptions
方法:顾名思义。在app开始运行时会调用里面的方法。官方文档的定义是"Tells
the delegate when the application has launched and may have
additional launch options to
handle."
它和didReceiveLocalNotification
这个方法的区别是,如果程序是在后台要启动至前台就调用didReceiveLocalNotification。如果程序完全退出,先调用didFinishLaunchingWithOptions把程序启动起来。
附带简单代码
- (BOOL)application:(UIApplication
*)application
didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions
{
// 获得类似首选项的用户初始化定义数据的引用
NSUserDefaults *prefs = [NSUserDefaults
standardUserDefaults];
//检查是否之前app运行过
if ([prefs
boolForKey:@"hasRunBefore"] !=
YES)
{
// 设置个标记使此if中字段只在第一次运行时执行
[prefs setBool:YES
forKey:@"hasRunBefore"];
[prefs synchronize];//
把设置的键值对同步
// Add our default user object in Core
Data。
//NSEntityDescription方法。在已经获得了managedObjectContext的前提下可以通
//过NSEntityDescription方法获得我们之前定义的entity。
Users *user =
(Users
*)[NSEntityDescription
insertNewObjectForEntityForName:@"Users"
inManagedObjectContext:self.managedObjectContext];
[user
setUsername:@"admin"];
[user
setPassword:@"password"];
// Commit to core data
NSError *error;
if
(![self.managedObjectContext
save:&error])
NSLog(@"Failed to add default user with
error: %@", [error
domain]);
}
// Pass the
managed object context to the root view controller (the login
view)
//LoginViewController是我们自己写的文件。rootView作为该类的应用,让delegate文件的rootViewController给LoginViewController赋值。再把delegate文件的managedObjectContext赋值给rootView。
使LoginViewController称为程序入口。
LoginViewController *rootView = (LoginViewController
*)self.window.rootViewController;
rootView.managedObjectContext =
self.managedObjectContext;
//加上下面的会导致一片空白
// self.window = [[UIWindow
alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// // Override point for
customization after application launch.
//
self.window.backgroundColor = [UIColor whiteColor];
// [self.window
makeKeyAndVisible];
return
YES;
}
相关文章推荐
- PyQt学习笔记(7)——Qt Design…
- Android App应用之发布各广告… 分类: Android开发 2014-05-30 10:54 101人阅读 评论(0) 收藏
- expect 使用笔记
- iOS APP设计规范大全
- 正则表达式 笔记
- 分享 康奈尔大学做笔记的方法
- Java学习笔记12 java中的多态、抽…
- Android笔记1 Android入门
- 编译原理笔记13 自底向上分析技术…
- Adobe FLEX学习笔记(3)-访问air …
- C# 如何获取读取项目资源中的数据&…
- myEclipse 项目有红感叹号
- 对于java web 项目中文乱码的解决…
- CentOS学习笔记1:< yum提示another app is currently holding the yum lock;waiting for it to exit >
- C++ Primer 笔记2
- 一步步教你上架IOS APP(转)
- fedora&nbsp;学习笔记
- 关于AndroidStudio运行项目时出现 Could not create plugin of type 'AppPlugin'.