
tips
ctl + button -> segue: show
seque identifier -> class name
command + = -> size調整
file -> new file -> Cocoa Class : 新しいclassを生成 -> utility custom classでclass設定
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var textField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.textField.text = ""
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func showAlert() {
let VERSION: Float = (UIDevice.currentDevice().systemVersion as NSString).floatValue
if VERSION >= 8.0 {
let alertController = UIAlertController(title: "Error", message: "Please enter your name", preferredStyle: .Alert)
let defaultAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
alertController.addAction(defaultAction)
self.presentViewController(alertController, animated: true, completion: nil)
} else {
let alert = UIAlertView()
alert.title = "Error"
alert.message = "Please enter your name"
alert.addButtonWithTitle("OK")
alert.show()
}
}
override func shouldPerformSegueWithIdentifier(identifier: String?, sender: AnyObject?) -> Bool {
if identifier == "showResult" {
if self.textField.text == "" {
self.showAlert()
return false
}
return true
}
return true
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showreResult" {
let resultViewController: ResultViewController = segue.destinationViewController as! ResultViewController
resultViewController.myName = self.textField.text
self.textField.resignFirstResponder()
}
}
}
import UIKit
class ResultViewController: UIViewController {
var myName: String = ""
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var scoreLabel: UILabel!
override func viewDidLoad(){
super.viewDidLoad()
self.nameLabel.text = "\(self.myName)の点数は..."
let score = arc4random_uniform(101)
self.scoreLabel.text = "\(score)点"
}
}