顯示具有 TextField 標籤的文章。 顯示所有文章
顯示具有 TextField 標籤的文章。 顯示所有文章

2015年6月17日 星期三

iOS Swift Login 練習 登入系統

iOS開發開始
先練習一個登入功能頁面
用到
Label元件
Button元件
TextField元件
頁面切換功能

1.先建立一個Single View Application空白專案

2.打開Main.storyboard 拉一個登入畫面

在右方再拉一個登入成功頁面,並在identity的Storyboard ID 輸入任意名稱,下方Use Storyboard ID 打勾。


3.ViewController.swift
import UIKit

class ViewController: UIViewController {
    
    @IBOutlet weak var account: UITextField! //輸入帳號
    @IBOutlet weak var password: UITextField! //輸入密碼
    @IBOutlet weak var info: UILabel! //訊息
    let myAccount = "apple" //驗證帳號
    let myPassword = "123" //驗證密碼
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    //登入
    @IBAction func login(sender: UIButton) {
        if account.text == myAccount && password.text == myPassword {
            self.info.text = "成功"
            
            //傳至下一頁面
            let viewController:UIViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("MainActivity")
            
            self.presentViewController(viewController, animated: false, completion: nil)
            // 返回 self.dismissViewControllerAnimated(true, completion: nil)
            
        }else{
            self.info.text = "輸入錯囉"
        }
    }
    
    //清除
    @IBAction func reset(sender: UIButton) {
        account.text = ""
        password.text = ""
    }
    
}

參考連結: https://coderwall.com/p/cjuzng/swift-instantiate-a-view-controller-using-its-storyboard-name-in-xcode