新建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
来源:oschina
链接:https://my.oschina.net/u/113421/blog/685988