現在、UItableViewのcellをタップすると画面遷移するようstoryboardを使わず、コードのみで書いています。しかし、UItableViewはSimulatorに表示されますが、画面遷移はしません。そこで、どのようにコードを記述すれば、画面遷移するようになりますか?
※色々なサイトを見てみるとstoryboardを使って画面遷移させるのは多いのですが、コードのみのものが見つかりません。
※一応下記にコードも載せておきます。
swift
1/*ViewController.swift */ 2import UIKit 3 4class ViewController: UIViewController, UITableViewDelegate ,UITableViewDataSource { 5 var myItems: NSArray = ["test1","test2","test3","test4"] 6 private var myTableView: UITableView! 7 8 override func viewDidLoad() { 9 super.viewDidLoad() 10 // Do any additional setup after loading the view, typically from a nib. 11 12 let barHeight: CGFloat = UIApplication.sharedApplication().statusBarFrame.size.height 13 let displayWidth: CGFloat = self.view.frame.width 14 let displayHeight: CGFloat = self.view.frame.height 15 16 myTableView = UITableView(frame: CGRect(x: 0, y: barHeight, width: displayWidth, height: displayHeight - barHeight)) 17 myTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "MyCell") 18 myTableView.dataSource = self 19 20 self.view.addSubview(myTableView) 21 } 22 23 /* 24 サイトを調べた結果、storyboadでviewcontrollerを追加し、segueを設定しましたが、遷移しませんでした。。。 25 */ 26 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 27 print("Num: \(indexPath.row)") 28 print("Value: \(myItems[indexPath.row])") 29 performSegueWithIdentifier("toSubViewController",sender: nil) 30 } 31 32 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 33 return myItems.count 34 } 35 36 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 37 let cell = tableView.dequeueReusableCellWithIdentifier("MyCell", forIndexPath: indexPath) 38 cell.textLabel!.text = "\(myItems[indexPath.row])" 39 return cell 40 } 41 42 override func didReceiveMemoryWarning() { 43 super.didReceiveMemoryWarning() 44 // Dispose of any resources that can be recreated. 45 } 46 47
swift
1/*subViewController.swift */ 2import UIKit 3 4class subViewController: UIViewController { 5 6 7 override func viewDidLoad() { 8 super.viewDidLoad() 9 let backButton:UIButton = UIButton(frame: CGRectMake(0,0,120,50)) 10 backButton.backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 1) 11 backButton.layer.masksToBounds = true 12 backButton.layer.borderColor = UIColor(red: 0, green: 0, blue: 0, alpha: 1).CGColor 13 backButton.layer.borderWidth = 2 14 backButton.layer.position = CGPoint(x: self.view.bounds.width/2, y: self.view.bounds.height - 50) 15 backButton.addTarget(self, action: "onClickButton:", forControlEvents: .TouchUpInside) 16 self.view.addSubview(backButton) 17 18 19 } 20 21 internal func onClickButton(sender: UIButton){ 22 let myViewController: UIViewController = ViewController() 23 self.navigationController?.pushViewController(myViewController, animated: true) 24 } 25 26 override func didReceiveMemoryWarning() { 27 super.didReceiveMemoryWarning() 28 } 29 30} 31
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。