Entertainer Engineering

人を楽しませられる技術者を目指すあおかびんのブログ

SwiftのUIButtonで画面遷移をする話

こんばんは、かびんさん@あおかびんです。

今日はSwiftのUIButtonで画面遷移を行ったので
それをメモ書きします。

override func viewDidLoad() {
    super.viewDidLoad()
    self.view.backgroundColor = backgroundColor
    
    for i in 0...2 {
        let task: UIButton = UIButton()
        let icast:CGFloat = CGFloat(i - 1)
        let action: Selector = Selector("touchButton" + String(i) + ":")
        task.backgroundColor = UIColor.whiteColor()
        task.layer.masksToBounds = true
        task.setTitle("task" + String(i), forState: UIControlState.Normal)
        task.setTitleColor(UIColor.blackColor(), forState: .Normal)
        task.addTarget(self, action: action, forControlEvents: .TouchUpInside)
        task.layer.masksToBounds = true
        task.frame = CGRectMake(width / 2 - fieldWidth / 2, height / 2 - fieldHeight + (40.0 * icast), fieldWidth, fieldHeight)
        self.view.addSubview(task)
    }
}

internal func touchButton0(sender: UIButton){
    // 遷移するViewを定義する.
    let editViewController: TaskEditViewController = TaskEditViewController(i: 0)
    editViewController.modalTransitionStyle = UIModalTransitionStyle.CoverVertical
    self.presentViewController(editViewController, animated: true, completion: nil)
}

タッチイベントを取得したらその名前のセレクタが実行されるという形式。

UITextfieldのデザインを調整したいんだよな・・・。

以上あおかびんでした。