Help us understand the problem. What is going on with this article?

猫年齢を人間年齢に換算する(DoubleとStringを相互変換)

More than 5 years have passed since last update.

アプリ画像

やったこと

  • 猫年齢<->人間年齢換算アプリを作る
  • 数値キーボードにクリアボタンを表示する
  • 画面タップでキーボードを非表示にする

Double<->Stringの変換が多少役に立つかと。

「詳細Objective-C iPhoneアプリ開発入門ノート」Chapter4-3の学習記録です。

コード

ViewController.swift
class ViewController: UIViewController {

    // 年齢テキストボックス
    @IBOutlet weak var inputAge: UITextField!
    // 換算後年齢ラベル
    @IBOutlet weak var resultAge: UILabel!
    // 自作クラス
    var mp = MyProc()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

    }

    // 換算ボタンを押す
    @IBAction func convertAge(sender: AnyObject) {

        var input = self.inputAge.text
        var res:NSString = mp.convertAgeFromCatToHuman(input)
        self.resultAge.text = res
    }

    // 背景をタップ
    @IBAction func bkgTapped(sender: UITapGestureRecognizer) {

        //キーボードを閉じる
        self.view.endEditing(true)
    }

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


}
MyProc.swift
class MyProc:NSObject {

    // 初期化
    override init() {

    }

    // 猫の年齢を人間の年齢に変換する
    func convertAgeFromCatToHuman(catAge: NSString) -> String{

        // Doubleに変換
        var catAgeDbl = catAge.doubleValue
        var hmnAge:Double = 0
        if (catAgeDbl <= 1) {
            hmnAge = catAgeDbl + 20
        } else if (catAgeDbl <= 5) {
            hmnAge = catAgeDbl * 6 + 15
        } else if (catAgeDbl <= 10) {
            hmnAge = catAgeDbl * 5 + 20
        } else if (catAgeDbl <= 15) {
            hmnAge = catAgeDbl * 4 + 30
        } else {
            hmnAge = catAgeDbl * 3 + 45
        }

        var hmnAgeStr = NSString(format: "%.1f", hmnAge)
        return hmnAgeStr
    }
}

ソースアップ

https://github.com/hanoopy/ios_study1024_2.git

わからない

  • コンソールにエラーが出る
    2014-10-24 22:49:04.462 CatAge[2506:72454] Can't find keyplane that supports type 8 for keyboard iPhone-Portrait-DecimalPad; using 1425143906_Portrait_iPhone-Simple-Pad_Default

  • 本にはテキストボックスの入力制御をdelegateでやってたんだけど、delegateメソッドの書き方がわからない。ので今回はやめた。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした