11.9标签视图控制器

//AppDelegate.swift

importUIKit

importCoreData

@UIApplicationMain

classAppDelegate:UIResponder,UIApplicationDelegate{

varwindow:UIWindow?

funcapplication(application:UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject:AnyObject]?) ->Bool{

//创建一个标签视图控制器

letmainTabBarController =UITabBarController()

letfirstVC =ViewController()

letsecondVC =secondViewController()

letthirdVC =thirdViewController()

letfourthVC =fourthViewController()

letfifthVC =fifthViewController()

letsixthVC =sixViewController()

letseventhVC =seventhViewController()

//创建导航视图控制器,并且将firsrVC为根视图

letfirstNavc =UINavigationController(rootViewController: firstVC)

letsecondNavc =UINavigationController(rootViewController: secondVC)

letthirdNavc =UINavigationController(rootViewController: thirdVC)

//设置标签视图控制器的子视图控制器数组

mainTabBarController.viewControllers= [firstNavc,secondNavc,thirdNavc,fourthVC,fifthVC,sixthVC,seventhVC]

//设置代理监听标签选择的过程

mainTabBarController.delegate=self

firstNavc.tabBarItem.title="首页"

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

//设置可见

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{

//点击某一个标签时触发的方法

functabBarController(tabBarController:UITabBarController, didSelectViewController viewController:UIViewController) {

//标签控制器选中的item下标

println(tabBarController.selectedIndex)

//选中的是哪个控制器

println(viewController)

}

}



//ViewController.swift:

importUIKit

classViewController:UIViewController{

overridefuncviewDidLoad() {

super.viewDidLoad()

navigationItem.title="firstVC"

}

overridefuncdidReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

// Dispose of any resources that can be recreated.

}

}

你可能感兴趣的:(11.9标签视图控制器)