タイトルの通りswiftのコードによる画面遷移がなかなかうまくいきません。
最初に始めるというボタンを生成してそのボタンを押して次の画面へ遷移させたいです。
遷移させたい画面はViewController2でstoryboardIDはnextにしています。
他の同じような質問も見て同じようにしたのですがうまくいきません。
ご教示お願いします。
ボタンはきちんと生成されています。
###該当のソースコード
import
1 2class ViewController: UIViewController { 3 4 5let sentoral = UIButton() 6 7 func next(sender:UIButton){ 8 let nextViewController = self.storyboard?.instantiateViewControllerWithIdentifier("next2") as! ViewController2 9 navigationController?.pushViewController(nextViewController, animated: true) 10 } 11 12 override func viewDidLoad() { 13 super.viewDidLoad() 14 // Do any additional setup after loading the view, typically from a nib. 15 sentoral.frame = CGRect(x: 50,y: 400,width: 100,height: 21) 16 sentoral.setTitle("はじめる", forState: .Normal) 17 sentoral.backgroundColor = UIColor.grayColor() 18 sentoral.setTitleColor(UIColor.blackColor(), forState: .Normal) 19 sentoral.addTarget(self, action: #selector(ViewController.next(_:)), forControlEvents: UIControlEvents.TouchUpInside) 20 view.addSubview(sentoral) 21 } 22 23 override func didReceiveMemoryWarning() { 24 super.didReceiveMemoryWarning() 25 // Dispose of any resources that can be recreated. 26 } 27 28 29}
###補足情報(言語/FW/ツール等のバージョンなど)
swift2 xcode7
回答2件
あなたの回答
tips
プレビュー