随便

3.应用程序界面框架搭建

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        // 创建window
        self.window = UIWindow.init(frame: UIScreen.mainScreen().bounds)
        self.window?.backgroundColor = UIColor.whiteColor()
        
        
//        self.window?.rootViewController = YTTabBarController()
        
        
        if NSUserDefaults.standardUserDefaults().boolForKey("isOpenBefores") {
            
            //将标签栏控制器作为window的根视图控制器
            self.window?.rootViewController = YTTabBarController()
            
        }else{
            
            //设置本地数据,记录程序已经打开过
            NSUserDefaults.standardUserDefaults().setBool(true, forKey: "isOpenBefores")
            //将引导页作为window的根视图控制器
            self.window?.rootViewController = LeadViewController()
        }

        return true
    }


}



import UIKit

class YTTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.creatControllers()
        self.tabBarItemSetting()
        
    }
    // 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()
        let nav1 = YTNavigationController(rootViewController: one)
        one.title = "one"
        
        let two = TwoViewController()
        let nav2 = YTNavigationController(rootViewController: two)
        two.title = "two"
        
        let three = ThreeViewController()
        let nav3 = YTNavigationController(rootViewController: three)
        three.title = "three"
        
        // 将导航控制器对象作为标签
        self.viewControllers = [nav1,nav2,nav3]
        
    }

    
}



import UIKit

class LeadViewController: YTViewController {

    let imageName = "4.jpg"
    var imageview = UIImageView()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        //设置背景颜色
        self.view.backgroundColor = UIColor.greenColor()
        
        
        imageview = UIImageView.init(frame: self.view.bounds)
        imageview.image = UIImage.init(contentsOfFile: NSBundle.mainBundle().pathForResource(self.imageName, ofType: nil)!)
        self.view.addSubview(imageview)
        
        
        
        // 添加跳过按钮
        let btn = UIButton.init(frame: CGRectMake(300, 30, 100, 50))
        btn.setTitle("跳过", forState: .Normal)
        btn.addTarget(self, action: "btnAction", forControlEvents: .TouchDragInside)
        self.view.addSubview(btn)
    }
    
    func btnAction() {
        
        print("跳过")
        
        //添加转场动画
        self.view.addTransitionAnimation(0.5, type: TransitionType.RippleEffect, direction: TransitionDirection.FromBottom)
        
        //跳到主界面
        self.view.window?.rootViewController = YTTabBarController()
    }

    

}


2 . 标签栏视图控制器基础
import UIKit

class YTTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        self.creatControllers()
        self.tabBarSetting()
        self.tabBarItemSetting()

        
    }

    
    //MARK: - 定制tabBar(高度49)
    func tabBarSetting() {
        
        
        // 1. 设置是否透明(默认true ——> 透明)
        self.tabBar.translucent = false
        // 2. 设置背景颜色
        self.tabBar.barTintColor = UIColor.whiteColor()
        // 3. 设置背景图片
//        self.tabBar.backgroundImage = UIImage.init(named: "屏幕快照 2016-09-01 下午3.21.46.png")
        
        // 4. 设置填充颜色
        self.tabBar.tintColor = UIColor.orangeColor()
       
        
        
        
        
        
    }
    
    
    //MARK: - 定制 tabBarItem
    func tabBarItemSetting() {
        
        // 拿到tabBar上所有的tabBarItem对象
        let items = self.tabBar.items
        
        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(15),NSForegroundColorAttributeName:UIColor.lightGrayColor()], forState: .Normal)
            item.setTitleTextAttributes([NSFontAttributeName:UIFont.systemFontOfSize(15),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]
        
    }
   

}


import UIKit

class YTViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

       self.creatUI()
    }

    
    func creatUI() {
        
       
        
        //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)
    }
}



import UIKit

class ThereViewController: YTViewController {

    let imageName = "6.jpg"
    var imageview = UIImageView()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        imageview = UIImageView.init(frame: self.view.bounds)
        imageview.image = UIImage.init(contentsOfFile: NSBundle.mainBundle().pathForResource(self.imageName, ofType: nil)!)
        self.view.addSubview(imageview)
    }
}

1.

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        // 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 there = ThereViewController()
        there.title = "there"
        
        let four = FourViewController()
        four.title = "four"
        
        let five = FiveViewController()
        five.title = "five"
        
        let six  = SixViewController()
        six.title = "six"
        
        
        
        
        
        // c. 将视图控制器交给标签栏控制器管理
        // 标签控制器会自动创建每个视图控制器对应的标签
        // 注意: 标签栏控制器的标签栏上最多能显示5个标签。如果有超过5个子视图控制器,那么第5个和超出的视图控制器的标签会被“more”标签代替
        tabBarC.viewControllers = [one,two,there,four,five,six]
        
        
        
        // d. 设置默认选中的标签
        tabBarC.selectedIndex = 2
        
        
        
        // 3. 将标签栏控制器作为window的根视图控制器
        self.window?.rootViewController = tabBarC
        
        
        
        return true
    }



import UIKit

class YTViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

       self.creatUI()
    }

    
    func creatUI() {
        
        //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)
    }


4. 登录与注册


// 将数据保存到手机硬盘中-> 数据本地化
// IOS的数据本地化
// 1. 文件操作(NSFileManager和 NSFileHandle)
// 2. plist文件(NSUserDefaults)
// 3. 数据库
// 4. 归档和解归档

// IOS应用程序中的本地路径 -> 涉盒路径
// 苹果手机上安装的每个应用程序都有一个专门的内存空间用来存储当前应用程序中产生的本地数据,这个内容空间就是沙盒。每个应用程序对应的沙盒是独立的,相互不影响。而且相互不能访问。



import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // 创建window
        self.window = UIWindow.init(frame: UIScreen.mainScreen().bounds)
        self.window?.backgroundColor = UIColor.whiteColor()
        
        
        let navC = YTNavigationController(rootViewController:MyViewController())
        self.window?.rootViewController = navC
        
        // 设置状态栏为白色
        UIApplication.sharedApplication().statusBarStyle = .LightContent
        
        
        return true
    }



import UIKit

class YTNavigationController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // 定制navigationBar
        // 1. 设置背景颜色
        self.navigationBar.barTintColor = UIColor.blackColor()
        
        // 2. 设置中间的文字颜色
        //NSForegroundColorAttributeName 文字颜色的key
        self.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()]
        
        // 3. 设置填充颜色
        self.navigationBar.tintColor = UIColor.whiteColor()
        
        
        
        
    }
}



import UIKit

class YTViewController: UIViewController {
    
    // MARK: - 属性:
    // 1. 背景图片名
    var bgImageName = "2.jpg"
    var bgImageView = UIImageView()

    // MARK: -  方法
    override func viewDidLoad() {
        super.viewDidLoad()
        self.creatUI()
    }

//    override func viewWillAppear(animated: Bool) {
//        super.viewWillAppear(animated)
//    }
    
    

    // MARK: - 创建界面
    func creatUI() {
        
        //1. 设置背景图片
        bgImageView = UIImageView.init(frame: self.view.bounds)
        bgImageView.image = UIImage.init(contentsOfFile: NSBundle.mainBundle().pathForResource(self.bgImageName, ofType: nil)!)
        self.view.addSubview(bgImageView)
        
    }
    
    //MARK: - 警告框
    func showAlert(text:String,action:((UIAlertAction)->Void)?) {
        
        // 创建警告框
        let  alert = UIAlertController.init(title: nil, message: text, preferredStyle: .Alert)
        
        //添加action
        let action = UIAlertAction.init(title: "确定", style: .Default, handler: action)
        
//        let action = UIAlertAction.init(title: "确定", style: .Default) { (_) in
//            print("点击确定按钮")
//        }
        alert.addAction(action)
        
        // 将警告框显示在界面上
        self.presentViewController(alert, animated: true, completion: nil)

}

}


import UIKit

class MyViewController: YTViewController {
    
    
    // 属性:
    let centerBtn = UIButton()
   
    

    override func viewDidLoad() {
        

        // 设置背景图片名
        self.bgImageName = "2.jpg"
        // 设置title
        self.title = "我"
        super.viewDidLoad()
        
        
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        // 判断当前的登录状态
        let loginState = NSUserDefaults.standardUserDefaults().boolForKey(LoginState)
        if loginState == true {
            
            self.centerBtn.setTitle("已登录", forState: .Normal)
            // 如果已经登录这个按钮不能点击
            self.centerBtn.userInteractionEnabled = false
            
        }
        else {
            
            self.centerBtn.setTitle("请登录", forState: .Normal)
            //如果已经登录这个按钮可以点击
            self.centerBtn.userInteractionEnabled = true
        }
    }
    
    
    // MARK: - 搭建界面
    override func creatUI() {
        
        super.creatUI()
        
        // 添加中间的按钮
        self.centerBtn.frame = CGRectMake(0, 0, 100, 100)
        self.centerBtn.center = self.view.center
        self.centerBtn.setTitle("请登录", forState: .Normal)
        self.centerBtn.backgroundColor = UIColor.init(red: 0, green: 0, blue: 0, alpha: 0.5)
        self.view.addSubview(self.centerBtn)
        
        // 切圆
        self.centerBtn.layer.masksToBounds = true
        self.centerBtn.layer.cornerRadius = 50
        
        // 添加按钮点击事件
        self.centerBtn.addTarget(self, action: "buttonAction:", forControlEvents: .TouchDown)
        

    }
    

}


extension MyViewController {
    
    func buttonAction(button: UIButton) {
        
        print("按钮被点击")
        
        // 进入登录界面
        let login = LoginViewController()
        self.navigationController?.pushViewController(login, animated: true)
        
    }
}



import UIKit


// 登录状态的key
let LoginState = "login_State"


class LoginViewController: YTViewController {
    
    //MARK: - 属性
    // 1.键盘出现或者消失时上移或者下移的内容的容器视图
    let bgView = UIView()
    // 2. 两只手(大手和小手)
    let leftHand1 = UIImageView.init(image: UIImage.init(named: "hand"))
    let rightHand1 = UIImageView.init(image: UIImage.init(named: "hand"))
    let leftHand2 = UIImageView.init(image: UIImage.init(named: "left"))
    let rightHand2 = UIImageView.init(image: UIImage.init(named: "right"))
    
    // 3. 输入框
    var userField = UITextField()
    var passField = UITextField()
    
    
    
    
    // MARK: - 生命周期
    override func viewDidLoad() {
        super.viewDidLoad()

        // 设置背景图片
        self.bgImageName = "3.jpg"
        super.viewDidLoad()
        
        //设置title
        self.title = "登录"
        
        // 注册成为观察者
        self.addObserverAction()
    }
    
    override func viewWillAppear(animated: Bool) {
        
        super.viewWillAppear(animated)
        // 取出本地存储的最近登录的账号名
        let user = NSUserDefaults.standardUserDefaults().objectForKey(currentUser) as? String
        
        self.userField.text = user
        
        
    }
    
    

    
    
    // MARK: - 创建界面
    override func creatUI() {
        super.creatUI()
        
        // 1. 设置navigationItem
        let item = UIBarButtonItem.init(title: "注册", style: .Plain, target: self, action: "registerAction")
        self.navigationItem.rightBarButtonItem = item
        
        // 2. 创建登录界面
        // a. 透明的背景面
        self.bgView.frame = CGRectMake(20, 167, 338, 353)
//        self.bgView.backgroundColor = UIColor.yellowColor()
        self.view.addSubview(self.bgView)
        
        // b. 猫头
        let headImageView = UIImageView.init(frame: CGRectMake(65, 24, 205, 105))
        headImageView.image = UIImage.init(named: "header")
        
        self.bgView.addSubview(headImageView)
        // b2. 两只大手
        leftHand2.frame = CGRectMake(82, 128, 40, 65)
        rightHand2.frame = CGRectMake(230, 127, 40, 65)
        self.bgView.addSubview(leftHand2)
        self.bgView.addSubview(rightHand2)
        
        // c. 输入框所在的框
        let frameView = UIView.init(frame: CGRectMake(0, 121, 338, 152))
        frameView.backgroundColor = UIColor.init(red: 242/255.0, green: 242/255.0, blue: 242/255.0, alpha: 1)
        self.bgView.addSubview(frameView)
        
        // d. 两只小手
        self.leftHand1.frame = CGRectMake(73, 103, 37, 37)
        self.bgView.addSubview(leftHand1)
        self.rightHand1.frame = CGRectMake(230, 103, 37, 37)
        self.bgView.addSubview(rightHand1)
        
        // e. 两个输入框
        // 账号
        userField = UITextField.init(frame: CGRectMake(31, 135, 274, 50))
        userField.borderStyle = .RoundedRect
        userField.placeholder = "请输入账号"
        
        let leftImageView1 = UIImageView.init(frame: CGRectMake(0, 0, 40, 40))
        leftImageView1.image = UIImage.init(named: "user")
        userField.leftView = leftImageView1
        userField.leftViewMode = .Always
        self.bgView.addSubview(userField)
        userField.delegate = self
        
        // 密码
        passField = UITextField.init(frame: CGRectMake(31, 200, 274, 50))
        passField.borderStyle = .RoundedRect
        passField.placeholder = "请输入密码"
        passField.secureTextEntry = true
        let leftImageView2 = UIImageView.init(frame: CGRectMake(0, 0, 40, 40))
        leftImageView2.image = UIImage.init(named: "pass")
        passField.leftView = leftImageView2
        passField.leftViewMode = .Always
        self.bgView.addSubview(passField)
        passField.delegate = self
        
        // 按钮
        let loginBtn = UIButton.init(frame: CGRectMake(26, 288, 287, 40))
        loginBtn.setTitle("登录", forState: .Normal)
        loginBtn.backgroundColor = UIColor.init(red: 254/255.0, green: 189/255.0, blue: 151/255.0, alpha: 1)
        loginBtn.layer.masksToBounds = true
        loginBtn.layer.cornerRadius = 6
        loginBtn.addTarget(self, action: "loginAction", forControlEvents: .TouchDown)
        self.bgView.addSubview(loginBtn)

        
    }

    

}

//MARK: - 按钮点击
extension LoginViewController {
    func loginAction(button:UIButton) {
        
        // 判断是否输入账号
        if self.userField.text?.characters.count == 0 {
            self.showAlert("请输入账号", action: nil)
        }
        //判断是否输入密码
        if self.passField.text?.characters.count == 0 {
            self.showAlert("请输入密码", action: nil)
        }
        
        //通过当前输入的账号去获取保存到这个账号对应密码
        let pass = NSUserDefaults.standardUserDefaults().objectForKey(self.userField.text!) as? String
        
        // 判断账号是否存在
        if pass == nil {
            self.showAlert("账号不存在", action: nil)
            return
        }
        
        //判断当前
        if pass == self.passField.text {
            self.showAlert("密码错误", action: nil)
            return
        }
        
        // 登录成功
        // a. 更新最近登录的账号
        NSUserDefaults.standardUserDefaults().setObject(self.userField.text, forKey: currentUser)
        // b. 更新登录状态
        NSUserDefaults.standardUserDefaults().setBool(true, forKey: LoginState)
        // c. 显示登录成功。按确定按钮返回到我的界面
        self.showAlert("登录成功") { (_) in
            self.navigationController?.popViewControllerAnimated(true)
        }
        
        
    }
}


// 监听键盘的弹起和收起
extension LoginViewController {
    func addObserverAction() {
        
        // 当键盘弹起和收起的时候,系统会自动通过消息中心发送相应的消息.
        //UIKeyboardWillShowNotification -> 键盘将要弹起对应的消息名
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "showKeyBoard:", name: UIKeyboardWillShowNotification, object: nil)
        //UIKeyboardWillHideNotification -> 键盘将要收起对应的消息名
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "hideKeyBoard:", name: UIKeyboardWillHideNotification, object: nil)
    }
    
    //键盘弹起
    func showKeyBoard(nof:NSNotification) {
        print("键盘弹起")
        
        UIView.animateWithDuration(0.4) { () -> Void in
            self.bgView.transform = CGAffineTransformMakeTranslation(0, -110)
            }
    }
    
    //键盘收起
    func hideKeyBoard(nof:NSNotification) {
        print("键盘收起")
        UIView.animateWithDuration(0.4) { () -> Void in
            self.bgView.transform = CGAffineTransformMakeTranslation(0, 0)
        }

    }
}



// MARK: - 按钮点击
extension LoginViewController {
    
    //跳转到注册界面
    func registerAction() {
        
        let story = UIStoryboard.init(name: "Register", bundle: nil)
        let register = story.instantiateInitialViewController()
        self.navigationController?.pushViewController(register!, animated: true)
        
        
    }
    
}

//MARK: - textField Delegate
extension LoginViewController:UITextFieldDelegate {
    
    func textFieldDidBeginEditing(textField: UITextField) {
        
        //如果是密码输入框开始编辑
        if textField == passField {
            
            // 设置用手遮住眼睛的动画效果
            UIView.animateWithDuration(0.4, animations: { () -> Void in
                self.leftHand2.frame = CGRectMake(122, 81, 40, 65)
                self.rightHand2.frame = CGRectMake(180, 85, 40, 65)
                self.leftHand1.frame = CGRectMake(122, 85, 37, 37)
                self.rightHand1.frame = CGRectMake(186, 86, 37, 37)
                
                }, completion: { (_) -> Void in
                    // 动画结束的时候隐藏小手
                    self.leftHand1.hidden = true
                    self.rightHand1.hidden = true
                    
            })
            return
        }
        
        // 点击其他输入框
        self.outHandAnimation()
    }
    
    
    
    // 手展开的动画效果方法
    func outHandAnimation() {
        
        
            self.leftHand1.hidden = false
            self.rightHand1.hidden = false
            
            UIView.animateWithDuration(0.4, animations: { () -> Void in
                self.leftHand1.frame = CGRectMake(73, 103, 37, 37)
                self.rightHand1.frame = CGRectMake(230, 103, 37, 37)
                self.leftHand2.frame = CGRectMake(82, 128, 40, 65)
                self.rightHand2.frame = CGRectMake(230, 127, 40, 65)
                
                
                })  { (_)  in
                    
                    
            }
            
            
            }
    }
    




//MARK: -> UITouch
// 键盘收起
extension LoginViewController {
    
    //触摸当前视图控制器的view的时候会自动调用这个方法
    override func touchesBegan(touches: Set, withEvent event: UIEvent?) {
        self.view.endEditing(true)
    }
    
}



import UIKit

// 最近登录的账号对应的key
let currentUser = "userName_Current"

class RegisterViewController: YTViewController {
    
    //属性:
    @IBOutlet weak var userTextField: UITextField!
    
    @IBOutlet weak var passWordField: UITextField!

    @IBOutlet weak var passWordField2: UITextField!
    
    
    override func viewDidLoad() {
        //设置背景图片名
        self.bgImageName = "5.jpg"
        super.viewDidLoad()

        // 将背景图片放到最底层
        self.view.sendSubviewToBack(self.bgImageView)
        
        //    ==================沙盒================
        // !!!!!面试常问
        // 1. 拿到沙盒路径
        // 如果应用程序在真机上,拿到的就是真机上的沙盒目录,在模拟器上可以拿到模拟器对应的沙盒目录
        // a. Documents:存到这个目录的数据除非将应用程序卸载,否则里面的数据不会自动销毁
        let documentPath = NSHomeDirectory() + "/Documents"
        
        // b. Library:在程序更新的时候,数据会自动被删除
        //  - Caches(缓存): 专门用来存储缓存数据,在清除缓存的时候就将这个文件夹的内容删除
        let cachesPath = NSHomeDirectory() + "/Library/Caches"
        //  - Preferences:专门用来存储设置性数据
         let preferencesPath = NSHomeDirectory() + "/Library/Preferences"
        // c. tmp:存到这个目录下的数据在程序结束后会自动销毁
        let tmp = NSHomeDirectory() + "/tmp"
        
        print(NSHomeDirectory())
        
        
        
        //==============NSUserDefaults============
        
        //使用NSUserDefaults可以快速的通过plist文件将数据存储到本地(沙盒目录)。只能存储数组、字典、字符串、 数字、bool值、NSdate、NSData
        // 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、NSData)存到本地的plist文件中
        defaults.setObject("aaa", forKey: "string")
        // 将整数存到本地的plist文件中
        defaults.setInteger(100, forKey: "int")
        
        // 2. 将本地数据取出来
        print(defaults.objectForKey("string"))
        print(defaults.objectForKey("int"))
       
    }
}

//MARK: - 登录按钮点击
    extension RegisterViewController {
        
        @IBAction func registerAction(sender: UIButton) {
            // 判断账号是否为空
            if self.userTextField.text?.characters.count == 0 {
                //用警告框提示账号为空
                
                self.showAlert("账号为空", action: nil)
                
                return
            }
            //判断密码的长度是否小于6
            if self.passWordField.text?.characters.count < 6 {
                self.showAlert("密码的长度必须大于6", action: nil)
                return
            }
            // 判断两次输入的密码是否一致
            if self.passWordField.text != self.passWordField2.text {
                self.showAlert("两次输入的密码不相同", action: nil)
                return
            }
            
            //注册成功  
            // 1. 保存数据到本地
            // a. 账号:密码
            NSUserDefaults.standardUserDefaults().setObject(self.passWordField.text, forKey: self.userTextField.text!)
            // b. 最近登录的账号:账号
            NSUserDefaults.standardUserDefaults().setObject(self.userTextField.text, forKey: currentUser)
            
            
            
            
            
            
            // 2. 提示已经注册成功。并点击确定按钮跳转到登录页面
            
            
            self.showAlert("注册成功") { (_) in
                
                //回到登录页面
                self.navigationController?.popViewControllerAnimated(true)
            }
            
        }
    }


// MARK: - UITouch
extension RegisterViewController {
    
    override func touchesBegan(touches: Set, withEvent event: UIEvent?) {
        
        // 收起键盘
        self.view.endEditing(true)
    }
}

你可能感兴趣的:(随便)