Skip to content

Instantly share code, notes, and snippets.

@chuck0523
Created June 24, 2016 11:27
Show Gist options
  • Save chuck0523/e8d14afaa7873f07f42c7bd9e5c23a17 to your computer and use it in GitHub Desktop.
Save chuck0523/e8d14afaa7873f07f42c7bd9e5c23a17 to your computer and use it in GitHub Desktop.
//
// ViewController.swift
// 004
//
// Created by chuck on 6/24/16.
// Copyright © 2016 chuck. All rights reserved.
//
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
private var myTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// UITextFieldを生成
myTextField = UITextField(frame: CGRectMake(0,0,200,30))
// 表示する文字を代入
myTextField.text = "Hello Swift!"
// Delegateを設定
myTextField.delegate = self
// 枠を設定する
myTextField.borderStyle = UITextBorderStyle.RoundedRect
// 位置を設定
myTextField.layer.position = CGPoint(x: self.view.bounds.width/2, y:100)
// viewに追加
self.view.addSubview(myTextField)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// 編集直後に呼ばれるイベント
func textFieldDidBeginEditing(textField: UITextField) {
print("textFieldDidBeginEditing: " + textField.text!)
}
// 編集終了に呼ばれるイベント
func textFieldShouldEndEditing(textField: UITextField) -> Bool {
print("textFieldShouldEndEditing: " + textField.text!)
return true
}
// 改行イベント
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment