//AppDelegate.swift
importUIKit
importCoreData
@UIApplicationMain
classAppDelegate:UIResponder,UIApplicationDelegate{
varwindow:UIWindow?
funcapplication(application:UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject:AnyObject]?) ->Bool{
//創(chuàng)建一個(gè)標(biāo)簽視圖控制器
letmainTabBarController =UITabBarController()
letfirstVC =ViewController()
letsecondVC =secondViewController()
letthirdVC =thirdViewController()
letfourthVC =fourthViewController()
letfifthVC =fifthViewController()
letsixthVC =sixViewController()
letseventhVC =seventhViewController()
//創(chuàng)建導(dǎo)航視圖控制器,并且將firsrVC為根視圖
letfirstNavc =UINavigationController(rootViewController: firstVC)
letsecondNavc =UINavigationController(rootViewController: secondVC)
letthirdNavc =UINavigationController(rootViewController: thirdVC)
//設(shè)置標(biāo)簽視圖控制器的子視圖控制器數(shù)組
mainTabBarController.viewControllers= [firstNavc,secondNavc,thirdNavc,fourthVC,fifthVC,sixthVC,seventhVC]
//設(shè)置代理監(jiān)聽標(biāo)簽選擇的過程
mainTabBarController.delegate=self
firstNavc.tabBarItem.title="首頁(yè)"
secondNavc.tabBarItem.title="周邊"
thirdNavc.tabBarItem.title="我的"
fourthVC.tabBarItem.title="fourthVC"
fifthVC.tabBarItem.title="fifthVC"
sixthVC.tabBarItem.title="sixthVC"
seventhVC.tabBarItem.title="seventhVC"
//
window=UIWindow(frame:UIScreen.mainScreen().bounds)
window?.backgroundColor=UIColor.whiteColor()
//根視圖控制器
window?.rootViewController= mainTabBarController
//設(shè)置可見
window?.makeKeyAndVisible()
returntrue
}
funcapplicationWillResignActive(application:UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
funcapplicationDidEnterBackground(application:UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
funcapplicationWillEnterForeground(application:UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
funcapplicationDidBecomeActive(application:UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
funcapplicationWillTerminate(application:UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
// Saves changes in the application's managed object context before the application terminates.
self.saveContext()
}
// MARK: - Core Data stack
lazyvarapplicationDocumentsDirectory:NSURL= {
// The directory the application uses to store the Core Data store file. This code uses a directory named "bing._1_9_______" in the application's documents Application Support directory.
leturls =NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)
returnurls[urls.count-1]as!NSURL
}()
lazyvarmanagedObjectModel:NSManagedObjectModel= {
// The managed object model for the application. This property is not optional. It is a fatal error for the application not to be able to find and load its model.
letmodelURL =NSBundle.mainBundle().URLForResource("_1_9_______", withExtension:"momd")!
returnNSManagedObjectModel(contentsOfURL: modelURL)!
}()
lazyvarpersistentStoreCoordinator:NSPersistentStoreCoordinator? = {
// The persistent store coordinator for the application. This implementation creates and return a coordinator, having added the store for the application to it. This property is optional since there are legitimate error conditions that could cause the creation of the store to fail.
// Create the coordinator and store
varcoordinator:NSPersistentStoreCoordinator? =NSPersistentStoreCoordinator(managedObjectModel:self.managedObjectModel)
leturl =self.applicationDocumentsDirectory.URLByAppendingPathComponent("_1_9_______.sqlite")
varerror:NSError? =nil
varfailureReason ="There was an error creating or loading the application's saved data."
ifcoordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration:nil, URL: url, options:nil, error: &error) ==nil{
coordinator =nil
// Report any error we got.
vardict = [String:AnyObject]()
dict[NSLocalizedDescriptionKey] ="Failed to initialize the application's saved data"
dict[NSLocalizedFailureReasonErrorKey] = failureReason
dict[NSUnderlyingErrorKey] = error
error =NSError(domain:"YOUR_ERROR_DOMAIN", code:9999, userInfo: dict)
// Replace this with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog("Unresolved error\(error),\(error!.userInfo)")
abort()
}
returncoordinator
}()
lazyvarmanagedObjectContext:NSManagedObjectContext? = {
// Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.) This property is optional since there are legitimate error conditions that could cause the creation of the context to fail.
letcoordinator =self.persistentStoreCoordinator
ifcoordinator ==nil{
returnnil
}
varmanagedObjectContext =NSManagedObjectContext()
managedObjectContext.persistentStoreCoordinator= coordinator
returnmanagedObjectContext
}()
// MARK: - Core Data Saving support
funcsaveContext () {
ifletmoc =self.managedObjectContext{
varerror:NSError? =nil
ifmoc.hasChanges&& !moc.save(&error) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog("Unresolved error\(error),\(error!.userInfo)")
abort()
}
}
}
}
extensionAppDelegate:UITabBarControllerDelegate{
//點(diǎn)擊某一個(gè)標(biāo)簽時(shí)觸發(fā)的方法
functabBarController(tabBarController:UITabBarController, didSelectViewController viewController:UIViewController) {
//標(biāo)簽控制器選中的item下標(biāo)
println(tabBarController.selectedIndex)
//選中的是哪個(gè)控制器
println(viewController)
}
}
//ViewController.swift:
importUIKit
classViewController:UIViewController{
overridefuncviewDidLoad() {
super.viewDidLoad()
navigationItem.title="firstVC"
}
overridefuncdidReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}