加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift学习SayHiApp

发布时间:2020-12-14 06:56:12 所属栏目:百科 来源:网络整理
导读:新建iOS single view application 名字为SayHiApp,打开main storyboard选中view controoler,右上角,attribute inspector中simulated metrics 的size 选择iphone 4.7-inch这样view controller更像是一个iphone.. 然后拖动三个控件到界面上lable,text field,bu

新建iOS single view application 名字为SayHiApp,打开main storyboard选中view controoler,右上角,attribute inspector中simulated metrics 的size 选择iphone 4.7-inch这样view controller更像是一个iphone..

然后拖动三个控件到界面上lable,text field,button

最后打开assistant editor,ctrl 拖动这三个控件到viewController.swift中,会自动生成如下代码

@IBOutlet weak var messsageLabel: UILabel!

@IBOutlet weak var nameField: UITextField!

@IBAction func buttonPressed(sender: UIButton)

最后修改完整的ViewController.swift代码如下

//
//  ViewController.swift
//  SayHiApp
//
//  Created by cyper on 6/2/16.
//  Copyright ? 2016 Moaz Tech. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var messsageLabel: UILabel!
    @IBOutlet weak var nameField: UITextField!
    @IBAction func buttonPressed(sender: UIButton) {
        if let name = nameField.text {
             messsageLabel.text = "Hi there (name)"
        }
       
        nameField.text = ""
        
        // 当text field获得焦点时显示软键盘,做如下设置:
        // Simulator > Hardware > Keyboard > Toggle Software Keyboard
        
        // 当点击Say Hi按钮后隐藏软键盘:
        // self.view.endEditing(true)
        // 或者
        nameField.resignFirstResponder()
    }
    
    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.
    }


}

模拟器选择iphone 6s,运行.


学习了怎么修改simulated metrics的size,怎么绑定控件到ViewController,最后学习了怎么 显示 和 隐藏 软键盘

参考:http://stackoverflow.com/questions/24034786/resignfirstresponder-in-swift

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读