您的位置:首页 > 移动开发

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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: