Skip to content

Instantly share code, notes, and snippets.

@kenji272
Created July 12, 2015 01:05
Show Gist options
  • Save kenji272/e72de8719a81542719ff to your computer and use it in GitHub Desktop.
Save kenji272/e72de8719a81542719ff to your computer and use it in GitHub Desktop.
import UIKit
class SecondViewController: UIViewController {
let divisionsName: [String] = ["東京都", "神奈川県", "埼玉県", "千葉県"]
var divisionButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// ボタンを生成する
self.createDivisionButton()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func createDivisionButton() {
for (index, division) in enumerate(self.divisionsName) {
divisionButton = UIButton()
divisionButton.frame = CGRectMake(0, 0, 300, 50)
divisionButton.setTitle(division, forState: .Normal)
divisionButton.setTitleColor(UIColor.blueColor(), forState: UIControlState.Normal)
var yPosition: CGFloat = CGFloat(150 + (index * 40))
divisionButton.layer.position = CGPoint(x: self.view.frame.width/2, y: yPosition)
divisionButton.tag = index
divisionButton.addTarget(self, action: "divisionTap:", forControlEvents:.TouchUpInside)
//viewにボタンを追加する
self.view.addSubview(divisionButton)
}
}
// 選択した値を保持し、最初の画面に遷移
func divisionTap(sender: UIButton) {
var firstView : AnyObject = self.storyboard?.instantiateViewControllerWithIdentifier("firstView") as! UIViewController
// 選択された値をセット
var viewController = firstView as! ViewController
viewController.selectDivision = divisionsName[sender.tag]
self.presentViewController(viewController, animated: true, completion: nil)
}
}
import UIKit
class ViewController: UIViewController {
var selectDivision = "未選択"
@IBOutlet weak var divisionLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// ラベルに値をセット
self.divisionLabel.text = self.selectDivision
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBAction func cancel(sender: UIStoryboardSegue) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment