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