swift入门之Login界面初探

在一个应用中login界面是必不可少的。那么如何制作一个登录界面将是本节要主要介绍的。



main.storyboard中增加一个navigation controller,作为流程跳转的主要逻辑


在默认的viewcontroller中增加两个button用来写注册的逻辑,代码如下


//
//  ViewController.swift
//  swiftLogin
//
//  Created by Chi Zhang on 14/6/11.
//  Copyright (c) 2014年 Chi. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    
    @IBOutlet
    var loginButton:UIButton
    
    @IBOutlet
    var registerButton:UIButton
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        loginButton.addTarget(self,action:"loginButtonClick:",forControlEvents:.TouchUpInside)
        registerButton.addTarget(self,action:"registerButtonClick:",forControlEvents:.TouchUpInside);
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func loginButtonClick(sender:UIButton!) {
        println("login")
        var loginView:LoginViewController = self.storyboard.instantiateViewControllerWithIdentifier("login") as LoginViewController
        self.navigationController.pushViewController(loginView, animated:true)
    }
    
    func registerButtonClick(sender:UIButton!) {
        println("register")
        var registerView:RegisterViewController = self.storyboard.instantiateViewControllerWithIdentifier("register") as RegisterViewController
        self.navigationController.pushViewController(registerView, animated:true)

    }

}

别忘了关联到storyboard



效果如下:


以上的逻辑中点击立即登录会跳转到LoginViewController, 点击免费注册会跳转到RegisterViewController.

所以在Main.storyboard中增加两个ViewController,分别命名为login和register




新建两个类分别命名为LoginViewController和RegisterViewController。

代码分别如下:


//
//  LoginViewController.swift
//  swiftLogin
//
//  Created by Chi Zhang on 14/6/11.
//  Copyright (c) 2014年 Chi. All rights reserved.
//

import UIKit

class LoginViewController: UIViewController , UITextFieldDelegate{

    init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        // Custom initialization
    }
    
    init(coder aDecoder: NSCoder!) {
        super.init(coder:aDecoder)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        var backgroundView:UIView = UIView(frame:CGRectMake(10,74,300,80))
        backgroundView.layer.cornerRadius = 8.0
        backgroundView.backgroundColor = UIColor(red: 247.0/255.0, green: 247.0/255.0, blue: 247.0/255.0, alpha: 1.0)
        backgroundView.layer.borderWidth = 1
        let borderColor:UIColor = UIColor(red: 209.0/255.0, green: 209.0/255.0, blue: 209.0/255.0, alpha: 1.0)
        backgroundView.layer.borderColor = borderColor.CGColor
        self.view.addSubview(backgroundView)
        
        var lineView:UIView = UIView(frame:CGRectMake(10,115,300,1))
        lineView.backgroundColor = borderColor
        self.view.addSubview(lineView)
        
        var evView:UIImageView = UIImageView(frame:CGRectMake(0,0,250,40))
        evView.userInteractionEnabled = true
        backgroundView.addSubview(evView)
        
        
        var lpFont:UIFont = UIFont.boldSystemFontOfSize(16);
        
        var unameLable:UILabel = UILabel(frame:CGRectMake(20,5,55,40))
        unameLable.text = "用户名"
        unameLable.highlighted = true
        unameLable.highlightedTextColor = UIColor.blackColor()
        unameLable.textColor = UIColor.blackColor()
        unameLable.backgroundColor = UIColor.clearColor()
        unameLable.font = lpFont
        backgroundView.addSubview(unameLable)
//        self.view.bringSubviewToFront(unameLable)
        
        var emailView = UITextField(frame:CGRectMake(80.0,5,200,40))
        emailView.backgroundColor = UIColor.clearColor()
        emailView.keyboardType = UIKeyboardType.EmailAddress
        emailView.textColor = UIColor.grayColor()
        emailView.tag = 101
        emailView.returnKeyType = UIReturnKeyType.Next
        emailView.font = UIFont.systemFontOfSize(17)
        emailView.delegate = self
        emailView.placeholder = "用户名或电子邮箱"
        emailView.highlighted = true
        evView.addSubview(emailView)
        
        
        var passwdLable:UILabel = UILabel(frame:CGRectMake(20, 43, 45, 40))
        passwdLable.backgroundColor = UIColor.clearColor()
        passwdLable.text = "密码"
        passwdLable.font = lpFont
        passwdLable.highlighted = true
        passwdLable.highlightedTextColor = UIColor.blackColor();
        passwdLable.backgroundColor = UIColor.clearColor()
        passwdLable.textColor = UIColor.blackColor()
        backgroundView.addSubview(passwdLable)
        
        var passwdView = UITextField(frame:CGRectMake(80, 43, 200, 40));
        passwdView.backgroundColor = UIColor.clearColor();
        passwdView.keyboardType = UIKeyboardType.Default;
        passwdView.borderStyle = UITextBorderStyle.None;
        passwdView.autocapitalizationType = UITextAutocapitalizationType.None; //关闭首字母大写
        passwdView.returnKeyType = UIReturnKeyType.Done; //完成
        passwdView.secureTextEntry = true //验证
        passwdView.delegate = self;
        passwdView.tag = 102;
        passwdView.textColor = UIColor.grayColor();
        passwdView.font = lpFont
        backgroundView.addSubview(passwdView)
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    /*
    // #pragma mark - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue?, sender: AnyObject?) {
        // Get the new view controller using [segue destinationViewController].
        // Pass the selected object to the new view controller.
    }
    */

}

//
//  RegisterViewController.swift
//  swiftLogin
//
//  Created by Chi Zhang on 14/6/24.
//  Copyright (c) 2014年 Chi. All rights reserved.
//

import UIKit

class RegisterViewController: UIViewController , UITextFieldDelegate{

    init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        // Custom initialization
    }
    
    
    init(coder aDecoder: NSCoder!) {
        super.init(coder:aDecoder)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        
        var backgroundView:UIView = UIView(frame:CGRectMake(10,74,300,125))
        backgroundView.layer.cornerRadius = 8.0
        backgroundView.backgroundColor = UIColor(red: 247.0/255.0, green: 247.0/255.0, blue: 247.0/255.0, alpha: 1.0)
        backgroundView.layer.borderWidth = 1
        let borderColor:UIColor = UIColor(red: 209.0/255.0, green: 209.0/255.0, blue: 209.0/255.0, alpha: 1.0)
        backgroundView.layer.borderColor = borderColor.CGColor
        self.view.addSubview(backgroundView)
        
        var lineView:UIView = UIView(frame:CGRectMake(10,115,300,1))
        lineView.backgroundColor = borderColor
        self.view.addSubview(lineView)
        
        var lpFont:UIFont = UIFont.boldSystemFontOfSize(16);
        
        var unameLable:UILabel = UILabel(frame:CGRectMake(20,5,55,40))
        unameLable.text = "用户名"
        unameLable.highlighted = true
        unameLable.highlightedTextColor = UIColor.blackColor()
        unameLable.textColor = UIColor.blackColor()
        unameLable.backgroundColor = UIColor.clearColor()
        unameLable.font = lpFont
        backgroundView.addSubview(unameLable)
        
        var unameView = UITextField(frame:CGRectMake(80.0,5,200,40))
        unameView.backgroundColor = UIColor.clearColor()
        unameView.keyboardType = UIKeyboardType.EmailAddress
        unameView.textColor = UIColor.grayColor()
        unameView.tag = 101
        unameView.returnKeyType = UIReturnKeyType.Next
        unameView.font = UIFont.systemFontOfSize(17)
        unameView.delegate = self
        unameView.placeholder = "用户名"
        unameView.highlighted = true
        backgroundView.addSubview(unameView)
        
        
        var passwdLable:UILabel = UILabel(frame:CGRectMake(20, 43, 45, 40))
        passwdLable.backgroundColor = UIColor.clearColor()
        passwdLable.text = "密码"
        passwdLable.font = lpFont
        passwdLable.highlighted = true
        passwdLable.highlightedTextColor = UIColor.blackColor();
        passwdLable.backgroundColor = UIColor.clearColor()
        passwdLable.textColor = UIColor.blackColor()
        backgroundView.addSubview(passwdLable)

        var passwdView = UITextField(frame:CGRectMake(80, 43, 200, 40));
        passwdView.backgroundColor = UIColor.clearColor();
        passwdView.keyboardType = UIKeyboardType.Default;
        passwdView.borderStyle = UITextBorderStyle.None;
        passwdView.autocapitalizationType = UITextAutocapitalizationType.None; //关闭首字母大写
        passwdView.returnKeyType = UIReturnKeyType.Next; //完成
        passwdView.secureTextEntry = true //验证
        passwdView.delegate = self;
        passwdView.tag = 102;
        passwdView.textColor = UIColor.grayColor();
        passwdView.font = lpFont
        backgroundView.addSubview(passwdView)
        
        var line2View:UIView = UIView(frame:CGRectMake(10, 155, 300, 1))
        line2View.backgroundColor = borderColor
        self.view.addSubview(line2View)
        
        var emailLable:UILabel = UILabel(frame:CGRectMake(20, 82, 280, 40))
        emailLable.backgroundColor = UIColor.clearColor()
        emailLable.text = "电子邮箱"
        emailLable.font = lpFont
        emailLable.highlighted = true
        emailLable.highlightedTextColor = UIColor.blackColor();
        emailLable.backgroundColor = UIColor.clearColor()
        emailLable.textColor = UIColor.blackColor()
        backgroundView.addSubview(emailLable)
        
        
        var emailView = UITextField(frame:CGRectMake(90, 82, 200, 40))
        emailView.backgroundColor = UIColor.clearColor()
        emailView.keyboardType = UIKeyboardType.EmailAddress
        emailView.textColor = UIColor.grayColor()
        emailView.tag = 103
        emailView.returnKeyType = UIReturnKeyType.Done
        emailView.font = UIFont.systemFontOfSize(17)
        emailView.delegate = self
        emailView.placeholder = "zhangchi1213@gmail.com"
        emailView.highlighted = true
        backgroundView.addSubview(emailView)
        
        var nextButton:UIBarButtonItem = UIBarButtonItem(title:"下一步", style:UIBarButtonItemStyle.Plain,target:self,action:"gotonext")
        self.navigationItem.rightBarButtonItem = nextButton
    }
    
    func gotonext() {
        println("go to next")
        var registerView:Register2ViewController = self.storyboard.instantiateViewControllerWithIdentifier("register2") as Register2ViewController
        self.navigationController.pushViewController(registerView, animated:true)

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func textFieldShouldReturn(_textField: UITextField!) -> Bool {
        _textField.resignFirstResponder()
        return true
    }
    
    func textFieldDidEndEditing(textField: UITextField!) {
        println(textField.text)
    }

    /*
    // #pragma mark - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue?, sender: AnyObject?) {
        // Get the new view controller using [segue destinationViewController].
        // Pass the selected object to the new view controller.
    }
    */

}

这里主要是增加两个界面,一个为登陆界面,包括用户名密码,另一个为注册界面,包括用户名密码邮箱,以及个人的详细信息等。


输入个人的详细信息需要单独的一个注册页面,新建另一个ViewController 命名为 Register2ViewController,代码如下:

//
//  Register2ViewController.swift
//  swiftLogin
//
//  Created by Chi Zhang on 14/6/25.
//  Copyright (c) 2014年 Chi. All rights reserved.
//

import UIKit

class Register2ViewController: UIViewController , UITextFieldDelegate{

    var nicknameView:UITextField!
    
    var birthDay:UIButton!
    
    init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        // Custom initialization
    }
    
    
    init(coder aDecoder: NSCoder!) {
        super.init(coder:aDecoder)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        var nextButton:UIBarButtonItem = UIBarButtonItem(title:"完成", style:UIBarButtonItemStyle.Plain,target:self,action:"finish")
        self.navigationItem.rightBarButtonItem = nextButton

        var backgroundView:UIView = UIView(frame:CGRectMake(10,74,300,125))
        backgroundView.layer.cornerRadius = 8.0
        backgroundView.backgroundColor = UIColor(red: 247.0/255.0, green: 247.0/255.0, blue: 247.0/255.0, alpha: 1.0)
        backgroundView.layer.borderWidth = 1
        let borderColor:UIColor = UIColor(red: 209.0/255.0, green: 209.0/255.0, blue: 209.0/255.0, alpha: 1.0)
        backgroundView.layer.borderColor = borderColor.CGColor
        self.view.addSubview(backgroundView)

        var lineView:UIView = UIView(frame:CGRectMake(10,115,300,1))
        lineView.backgroundColor = borderColor
        self.view.addSubview(lineView)
        
        var lpFont:UIFont = UIFont.boldSystemFontOfSize(16);
        
        var nicknameLable:UILabel = UILabel(frame:CGRectMake(20,5,75,40))
        nicknameLable.text = "宝贝小名"
        nicknameLable.highlighted = true
        nicknameLable.highlightedTextColor = UIColor.blackColor()
        nicknameLable.textColor = UIColor.blackColor()
        nicknameLable.backgroundColor = UIColor.clearColor()
        nicknameLable.font = lpFont
        backgroundView.addSubview(nicknameLable)

        nicknameView = UITextField(frame:CGRectMake(90.0,5,200,40))
        nicknameView.backgroundColor = UIColor.clearColor()
        nicknameView.keyboardType = UIKeyboardType.EmailAddress
        nicknameView.textColor = UIColor.grayColor()
        nicknameView.tag = 101
        nicknameView.returnKeyType = UIReturnKeyType.Next
        nicknameView.font = UIFont.systemFontOfSize(17)
        nicknameView.delegate = self
        nicknameView.placeholder = ""
        nicknameView.highlighted = true
        nicknameView.becomeFirstResponder()
        backgroundView.addSubview(nicknameView)
        
        var genderLable:UILabel = UILabel(frame:CGRectMake(20, 43, 75, 40))
        genderLable.backgroundColor = UIColor.clearColor()
        genderLable.text = "宝贝性别"
        genderLable.font = lpFont
        genderLable.highlighted = true
        genderLable.highlightedTextColor = UIColor.blackColor();
        genderLable.backgroundColor = UIColor.clearColor()
        genderLable.textColor = UIColor.blackColor()
        backgroundView.addSubview(genderLable)
        
        var genderView:UISegmentedControl = UISegmentedControl(items:["男","女"])
        genderView.frame = CGRectMake(90,46, 130,32)
        genderView.segmentedControlStyle = UISegmentedControlStyle.Plain
        genderView.selectedSegmentIndex = 1
        backgroundView.addSubview(genderView)
        
        var line2View:UIView = UIView(frame:CGRectMake(10, 155, 300, 1))
        line2View.backgroundColor = borderColor
        self.view.addSubview(line2View)
        
        var birthLabel:UILabel = UILabel(frame:CGRectMake(20, 86, 75, 40));
        birthLabel.text = "宝贝生日";
        birthLabel.highlighted = true;
        birthLabel.highlightedTextColor = UIColor.blackColor();
        birthLabel.font = lpFont;
        birthLabel.backgroundColor = UIColor.clearColor();
        birthLabel.textColor = UIColor.blackColor();
        backgroundView.addSubview(birthLabel);
        
        birthDay = UIButton(frame:CGRectMake(57, 86, 200, 40));
        birthDay.backgroundColor = UIColor.clearColor();
        birthDay.setTitleColor(UIColor.blackColor(), forState:UIControlState.Normal);
        birthDay.setTitle("2004-10-09",forState:UIControlState.Normal);
        birthDay.tag = 104;
        birthDay.addTarget(self,action:"openBirthday:",forControlEvents:.TouchUpInside);
        birthDay.highlighted = true
        backgroundView.addSubview(birthDay);
        
        var babyNote:UILabel = UILabel(frame:CGRectMake(45, 191, 300, 40));
        babyNote.backgroundColor = UIColor.clearColor();
        babyNote.textColor = UIColor(red:83.0/255.0,green:92.0/255.0,blue:112.0/255.0,alpha:1.0);
        babyNote.tag = 105;
        babyNote.font = UIFont.systemFontOfSize(14);
        babyNote.text = "推荐引擎会根据孩子信息进行个性推荐";
        babyNote.highlighted = true;
        self.view.addSubview(babyNote);

        //初始日期选择控件
        var datePicker:UIDatePicker = UIDatePicker(frame:CGRectMake(0.0, 200.0, 0.0, 0.0));
        var dateFormatter:NSDateFormatter = NSDateFormatter();
        dateFormatter.dateFormat = "yyyy-MM-dd";
        
        //将当前生日时间设置到日期轮轴上
        datePicker.date = dateFormatter.dateFromString(birthDay.titleLabel.text);
        
        //设置为中文显示
        var locale:NSLocale = NSLocale(localeIdentifier:"zh_CN");
        datePicker.locale = locale
        datePicker.datePickerMode = UIDatePickerMode.Date;
        
        datePicker.addTarget(self,action:"dataValueChanged:",forControlEvents:.ValueChanged);
        self.view.addSubview(datePicker);
    }
    
    func finish() {
        println("finish")
    }
    
    func openBirthday(sender:UIButton!) {
        println("openBirthday");
        nicknameView.resignFirstResponder()
    }
    
    func dataValueChanged(sender:UIDatePicker!) {
        println("dataValueChanged");
        var dateFormatter:NSDateFormatter = NSDateFormatter();
        dateFormatter.dateFormat = "yyyy-MM-dd";
        birthDay.setTitle(dateFormatter.stringFromDate(sender.date),forState:UIControlState.Normal)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    

    /*
    // #pragma mark - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue?, sender: AnyObject?) {
        // Get the new view controller using [segue destinationViewController].
        // Pass the selected object to the new view controller.
    }
    */

}



完整代码


没有更多推荐了,返回首页