// 將數(shù)據(jù)保存到手機(jī)硬盤(pán)中 -> 數(shù)據(jù)本地化
// ios中的數(shù)據(jù)本地化
// 1.文件操作(NSFileManager和NSFileHandle)
// 2.plist文件(NSUserDefaults)
// 3.數(shù)據(jù)庫(kù)
// 4.歸檔和解歸檔
// ios應(yīng)用程序中的本地路徑 -> 沙盒路徑
// 蘋(píng)果手機(jī)安裝的每個(gè)應(yīng)用程序都有一個(gè)專門(mén)的內(nèi)存空間用來(lái)存儲(chǔ)當(dāng)前應(yīng)用程序中產(chǎn)生的本地?cái)?shù)據(jù) ,這個(gè)內(nèi)容空間就是沙盒。每個(gè)應(yīng)用程序?qū)?yīng)的沙盒是獨(dú)立的,相互不影響,而且相互不能訪問(wèn)。
// =============沙盒===============
//M蛭B拚洹!面試常問(wèn)
// 1.拿到沙盒路徑
// 如果應(yīng)用程序是在真機(jī)上脚粟,拿到的就是真機(jī)的沙盒目錄覆旱,如果是在模擬器上可以拿到模擬器對(duì)應(yīng)的沙盒目錄
// a.Documents:存到這個(gè)目錄下的數(shù)據(jù)除非講應(yīng)用程序卸載,否則里面的數(shù)據(jù)會(huì)自動(dòng)銷毀
// let documentPath = NSHomeDirectory() + "/Douments"
// b.Library:在程序更新的時(shí)候核无,數(shù)據(jù)會(huì)自動(dòng)被刪除
// Caches:專門(mén)用來(lái)存儲(chǔ)緩存數(shù)據(jù) 在清除緩存的時(shí)候就將這個(gè)文件夾的內(nèi)容刪除
// let cachesPath = NSHomeDirectory() + "/Library/Caches"
// Preferences:(偏好設(shè)置)專門(mén)用來(lái)存儲(chǔ)設(shè)置性的數(shù)據(jù)
// let prefencesPath = NSHomeDirectory() + "/Library/Preferences"
// temp:存到這個(gè)目錄下的數(shù)據(jù)在程序結(jié)束后會(huì)自動(dòng)銷毀
// let temp = NSHomeDirectory() + "/temp"
print(NSHomeDirectory())
//================NSUserDefaults==============
//使用NSUserDefault可以快速的通過(guò)plist文件將數(shù)據(jù)存儲(chǔ)到本地文件(沙盒目錄)扣唱。只能存儲(chǔ)數(shù)組、字典团南、字符串噪沙、數(shù)字、Bool值吐根、NSData正歼、NSDate
//0.拿到NSUserDefaults對(duì)象(單例對(duì)象)
let defaults = NSUserDefaults.standardUserDefaults()
//1.將數(shù)據(jù)存到本地
//將Bool值存到本地的plist文件中
defaults.setBool(true, forKey: "bool")
//將浮點(diǎn)型數(shù)據(jù)存到本地的plist文件中
defaults.setFloat(10.9, forKey: "float")
defaults.setDouble(21.0, forKey: "double")
//將對(duì)象(字符串、NSData拷橘、NSDate)存到本地的plist文件中
defaults.setObject("aaa", forKey: "string")
//將整數(shù)存到本地plist文件中
defaults.setInteger(200, forKey: "int")
//
print(defaults.objectForKey("string"))
print(defaults.objectForKey("int"))
// 注冊(cè)成功
// 1.保存數(shù)據(jù)到本地
// a.賬號(hào):密碼
NSUserDefaults.standardUserDefaults().setObject(self.passWordField1.text, forKey: self.userTextField.text!)
// b.最近登錄的賬號(hào):賬號(hào)
NSUserDefaults.standardUserDefaults().setObject(self.userTextField.text, forKey: currentUser)
//取出本地存儲(chǔ)的最近登錄的賬號(hào)名
let user = NSUserDefaults.standardUserDefaults().objectForKey(currentUser) as? String
標(biāo)簽欄視圖控制器基礎(chǔ)
//UITabBarController:UIViewController
//UITabBarController是一個(gè)容器視圖控制器局义,專門(mén)用來(lái)管理其他的視圖控制器。如果將視圖圖控制器交給UITabBarController管理的話冗疮,UITabBarController會(huì)自動(dòng)在它的tabBar上創(chuàng)建一個(gè)對(duì)應(yīng)的標(biāo)簽萄唇,然后每次選中這個(gè)標(biāo)簽的時(shí)候,界面就會(huì)自動(dòng)切換到這個(gè)視圖控制器
//將視圖控制器交給標(biāo)簽欄控制器管理的方法:
//1.創(chuàng)建window
self.window = UIWindow.init(frame: UIScreen.mainScreen().bounds)
self.window?.backgroundColor = UIColor.whiteColor()
//2.創(chuàng)建標(biāo)簽欄控制器
//a.創(chuàng)建對(duì)象
let tabBarC = YTTabBarController()
//b.需要交給標(biāo)簽欄控制器管理的視圖控制器對(duì)象創(chuàng)建出來(lái)
let one = OneViewController()
one.title = "One"
let two = TwoViewController()
two.title = "Two"
let three = ThreeViewController()
three.title = "Three"
let four = FourViewController()
four.title = "Four"
let five = FiveViewController()
five.title = "Five"
let six = SixViewController()
six.title = "Six"
//c.將視圖控制器交給標(biāo)簽欄控制器管理
//標(biāo)簽控制器會(huì)自動(dòng)創(chuàng)建每個(gè)視圖控制器對(duì)應(yīng)的標(biāo)簽
//注意:標(biāo)簽欄控制器的標(biāo)簽欄上最多能顯示5個(gè)標(biāo)簽术幔。如果有超過(guò)5個(gè)子視圖控制器另萤,那么第五個(gè)和超出的視圖控制器的標(biāo)簽會(huì)被"more"標(biāo)簽代替
tabBarC.viewControllers = [one,two,three,four,five,six]
//d.設(shè)置默認(rèn)選中的標(biāo)簽
tabBarC.selectedIndex = 2
//3.將標(biāo)簽欄控制器作為window的根視圖控制器
self.window?.rootViewController = tabBarC
return true
//1.設(shè)置背景顏色
//CGFloat(arc4random()%256)/255
self.view.backgroundColor = UIColor.init(red: CGFloat(arc4random()%256)/255, green: CGFloat(arc4random()%256)/255, blue: CGFloat(arc4random()%256)/255, alpha: 1)
//UITabBarController的定制分兩個(gè)部分:
//1.tabBar -> 屬于標(biāo)簽欄控制器
//2.tabBarItem -> 屬于標(biāo)簽欄控制器管理的視圖控制器
//1.創(chuàng)建window
self.window = UIWindow.init(frame: UIScreen.mainScreen().bounds)
self.window?.backgroundColor = UIColor.whiteColor()
//2.創(chuàng)建標(biāo)簽欄控制器
//a.創(chuàng)建對(duì)象
let tabBarC = YTTabBarController()
//3.將標(biāo)簽欄控制器作為window的根視圖控制器
self.window?.rootViewController = tabBarC
//tabBarController
//MARK: - 定制tabBar(高度:49)
func tabBarSetting() {
//1.設(shè)置是否透明(默認(rèn)true->有透明度)
self.tabBar.translucent = true
//2.設(shè)置背景顏色
self.tabBar.barTintColor = UIColor.whiteColor()
//3.設(shè)置背景圖片
//self.tabBar.backgroundImage = UIImage.init(named: "bg")
//4.設(shè)置填充顏色
self.tabBar.tintColor = UIColor.orangeColor()
}
//MARK: - 定制tabBarItem
func tabBarItemSetting() {
//拿到tabBar上所有的tabBarItem對(duì)象
let items = self.tabBar.items
//創(chuàng)建所有的title和圖片名對(duì)應(yīng)的數(shù)組
let titles = ["條漫","繪本","專題","我的"]
let imageNames = ["tiaoman","huiben","zhuanti","wode"]
//設(shè)置item
for (i,item) in items!.enumerate() {
//設(shè)置標(biāo)題
item.title = titles[i]
//設(shè)置正常狀態(tài)的圖片
item.image = UIImage.init(named: imageNames[i]+"_u")?.imageWithRenderingMode(.AlwaysOriginal)
//設(shè)置選中狀態(tài)的圖片
item.selectedImage = UIImage.init(named: imageNames[i]+"_d")?.imageWithRenderingMode(.AlwaysOriginal)
//設(shè)置文字屬性
item.setTitleTextAttributes([NSFontAttributeName:UIFont.systemFontOfSize(13),NSForegroundColorAttributeName:UIColor.lightGrayColor()], forState: .Normal)
item.setTitleTextAttributes([NSFontAttributeName:UIFont.systemFontOfSize(13),NSForegroundColorAttributeName:UIColor.orangeColor()], forState: .Selected)
}
}
//MARK: - 創(chuàng)建子視圖控制器
func creatControllers() {
let one = OneViewController()
one.title = "one"
//設(shè)置tabBarItem
//a.設(shè)置文字
one.tabBarItem.title = "條漫"
//b.設(shè)置圖片
//正常狀態(tài)下的圖片
one.tabBarItem.image = UIImage.init(named: "tiaoman_u")?.imageWithRenderingMode(.AlwaysOriginal)
//選中狀態(tài)的圖片
one.tabBarItem.selectedImage = UIImage.init(named: "tiaoman_d")?.imageWithRenderingMode(.AlwaysOriginal)
let two = TwoViewController()
let there = ThereViewController()
let four = FourViewController()
self.viewControllers = [one,two,there,four]
}
應(yīng)用程序框架搭建
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
//創(chuàng)建window
self.window = UIWindow.init(frame: UIScreen.mainScreen().bounds)
self.window?.backgroundColor = UIColor.whiteColor()
if NSUserDefaults.standardUserDefaults().boolForKey("isOpenBefore") {
//將標(biāo)簽欄控制器作為window的根視圖控制器
self.window?.rootViewController = YTTabBarController()
}else{
//設(shè)置本地?cái)?shù)據(jù),記錄程序已經(jīng)打開(kāi)過(guò)
NSUserDefaults.standardUserDefaults().setBool(true, forKey: "isOpenBefore")
//將引導(dǎo)頁(yè)作為window的根視圖控制器
self.window?.rootViewController = LeadViewController()
}
return true
}
//MARK: - 定制tabBarItem
func tabBarItemSetting(){
//1.拿到所有的tabBarItem對(duì)象
let items = self.tabBar.items
//2.設(shè)置圖片
let imageNames = ["tiaoman","huiben","zhuanti"]
for (i,item) in items!.enumerate() {
item.image = UIImage.init(named: imageNames[i]+"_u")?.imageWithRenderingMode(.AlwaysOriginal)
item.selectedImage = UIImage.init(named: imageNames[i]+"_d")?.imageWithRenderingMode(.AlwaysOriginal)
}
//3.設(shè)置文字選中顏色
self.tabBar.tintColor = UIColor.orangeColor()
}
//MARK: - 創(chuàng)建子視圖控制器
func creatControllers() {
let one = OneViewController()
one.title = "one"
let nav1 = YTNavigationController(rootViewController: one)
let two = TwoViewController()
two.title = "two"
let nav2 = YTNavigationController(rootViewController: two)
let there = ThereViewController()
there.title = "there"
let nav3 = YTNavigationController(rootViewController: there)
//將導(dǎo)航控制器對(duì)象作為標(biāo)簽欄控制器的子視圖控制器
self.viewControllers = [nav1, nav2, nav3]
}
override func viewDidLoad() {
super.viewDidLoad()
//設(shè)置背景顏色
self.view.backgroundColor = UIColor.greenColor()
//添加跳過(guò)按鈕
let btn = UIButton.init(frame: CGRectMake(300, 30, 100, 50))
btn.setTitle("跳過(guò)", forState: .Normal)
btn.addTarget(self, action: "btnAction", forControlEvents: .TouchUpInside)
self.view.addSubview(btn)
}
func btnAction() {
//添加轉(zhuǎn)場(chǎng)動(dòng)畫(huà)
self.view.addTransitionAnimation(0.5, type: TransitionType.RippleEffect, direction: TransitionDirection.FromBottom)
//跳到主界面
self.view.window?.rootViewController = YTTabBarController()
}