Created
July 12, 2015 01:05
-
-
Save kenji272/e72de8719a81542719ff to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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