Git Product home page Git Product logo

ios-001_chinazodiac's Introduction

示例001、输入数字显示对应图片

  • 课程地址(幕课网-使用 Swift 开发第一章)

  • 代码地址(Github)

  • 技术分析

    1. 如图,在 Main.storyboard 中通过拖拽添加相关控件

    2. 按住 Ctrl + 右键拖拽至 ViewController.swift 在代码中关联控件

    3. 在代码中处理相关逻辑

  • 关键代码

    • 通过视图绑定控件

        @IBOutlet weak var yearNum: UITextField!
        ...
      


    • 通过视图绑定事件

         @IBAction func btnOk(sender: AnyObject) {
         	...
         }
      


    • 关闭键盘

        yearNum.resignFirstResponder()	
      


    • 点击屏幕其他地方以关闭键盘

        override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
            yearNum.resignFirstResponder()
        }
      


    • 处理逻辑

        // 获取输入的数字
        if let year = Int(yearNum.text!) {
            
            let imgNum = year % 12
            
             // 输入的数字模 12 得到对应图片设置给图片控件
            img.image = UIImage(named: String(imgNum))
            
            print(year)
            
        } else {
            print("invalide")
        }
      

ios-001_chinazodiac's People

Contributors

itcatface avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.