1. 加入 Navigation Controller,以 Seque = Push 來連接 Navigation Controller 與 Table View Controller. 首先,點選 Table View Controller,然後點選 Editor > Embed In > Navigation Controller: 這時 Navigation Controller 被自動設定為 Initial View Controller,且連結 Table View Controller。 2. 加入 View Controller 這 UI,以 Segue = Push 來連接 Table View Controller (Source) 與 View Controller (Destination) 先新增 View Controller,之後在 Table View Controller 選取 Cell,然後按下 Control 鍵不放,拖曳至新的 View Controller 上,此時設定 Segue = Push: 點選後,新的 Segue 連接了 Table View Controller 與 View Controller: 3. 在這新的 View Controller 的 Destination View Controller 上,新增 Lable 以顯示 Table View 被選取後的資料 接著給這新的 Segue 一個 ID: 在這之前,我們整個 UI 算是完成,接下來我們要控制這些 UI 元件。 4. 然後新增此 UI 物件的類別,將此類別 Assign 到此 UI (即這 UI 與這 Class 關聯) 新增 View Controller 類別: 然後新增此 UI 物件的類別,將此類別 Assign 到此 UI (即這 UI 與這 Class 關聯) 然後,將此類別 Assign 到此 UI : 5. 實作點選 Source Controller 上的 Cell後,在 Destination Controller 能顯示此資料: 點選 Destination Controller 上的 Label,並按 Control 鍵不放,拖曳至 Code 中: Detail View Controller 的程式修改如下: 接下來處理 Table View 資料選取後,傳遞到 View Controller 再處理,傳遞方式是 Source View Controller 會呼叫 prepareForSegue 這方法: 6. 在 Source View Controller 實作 prepareForSegue,當選取資料時,資料能傳遞到 Destination Controller 測試: Run 後顯示的 UI: 點選第一行資料 book,測試是否資料會傳遞到下一個 UI 畫面: 程式碼: Table View Controller:3. Implement Navigation, https://developer.apple.com/library/prerelease/ios/referencelibrary/GettingStarted/DevelopiOSAppsSwift/Lesson8.html// // MyTableViewController.swift // SimpleTableViewControllerDemo // // Created by Elvis Meng on 2015/9/6. // Copyright (c) 2015年 Elvis Meng. All rights reserved. // import UIKit class MyTableViewController: UITableViewController, UITableViewDataSource, UITableViewDelegate { var vocabulary = ["book","pen","queen","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15"] override func viewDidLoad() { super.viewDidLoad() // Uncomment the following line to preserve selection between presentations // self.clearsSelectionOnViewWillAppear = false // Uncomment the following line to display an Edit button in the navigation bar for this view controller. // self.navigationItem.rightBarButtonItem = self.editButtonItem() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } // MARK: - Table view data source override func numberOfSectionsInTableView(tableView: UITableView) -> Int { // #warning Potentially incomplete method implementation. // Return the number of sections. return 1 } override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { // #warning Incomplete method implementation. // Return the number of rows in the section. return self.vocabulary.count } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cellIdentifier = "Cell" let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! UITableViewCell // Configure the cell... cell.textLabel?.text = vocabulary[indexPath.row] return cell } override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { let cell: UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)! if cell.accessoryType == UITableViewCellAccessoryType.None { cell.accessoryType = UITableViewCellAccessoryType.Checkmark } else { cell.accessoryType = UITableViewCellAccessoryType.None } tableView.deselectRowAtIndexPath(indexPath, animated: true) } override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) { let cell: UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)! if cell.accessoryType == UITableViewCellAccessoryType.None { cell.accessoryType = UITableViewCellAccessoryType.Checkmark } else { cell.accessoryType = UITableViewCellAccessoryType.None } tableView.deselectRowAtIndexPath(indexPath, animated: false) } override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { if editingStyle == .Delete { self.vocabulary.removeAtIndex(indexPath.row) //tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) self.tableView.reloadData() } else if editingStyle == .Insert { self.vocabulary.insert("new", atIndex: indexPath.row) self.tableView.reloadData() } } override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? { var deleteAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Delete", handler: {(action:UITableViewRowAction!, indexPath: NSIndexPath!) -> Void in self.vocabulary.removeAtIndex(indexPath.row) self.tableView.reloadData() } ) let alertAction = UITableViewRowAction(style : UITableViewRowActionStyle.Default,title : "Share", handler : {(action:UITableViewRowAction!, indexPath: NSIndexPath!) -> Void in let alertController = UIAlertController(title:"My First App", message:"Hello World", preferredStyle: .Alert) let alertAction = UIAlertAction(title:"OK", style: UIAlertActionStyle.Default, handler:nil) alertController.addAction(alertAction) self.presentViewController(alertController, animated: true, completion: nil) } ) return [deleteAction, alertAction] } /* // Override to support editing the table view. override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { if editingStyle == .Delete { // Delete the row from the data source tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) } else if editingStyle == .Insert { // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view } } */ /* // Override to support rearranging the table view. override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) { } */ /* // Override to support conditional rearranging of the table view. override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool { // Return NO if you do not want the item to be re-orderable. return true } */ // MARK: - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. if segue.identifier == "detailSegue" { let indexPath = self.tableView.indexPathForSelectedRow() var destinationController = segue.destinationViewController as! DetailViewController destinationController.senderText = self.vocabulary[indexPath!.row] } } }
Detail View Controller:// // DetailViewController.swift // SimpleTableViewControllerDemo // // Created by Elvis Meng on 2015/9/12. // Copyright (c) 2015年 Elvis Meng. All rights reserved. // import UIKit class DetailViewController: UIViewController { @IBOutlet var word: UILabel! var senderText:String! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. self.word.text = senderText } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } /* // MARK: - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { // Get the new view controller using segue.destinationViewController. // Pass the selected object to the new view controller. } */ }
參考: 1. Segue Compatibility, https://developer.apple.com/library/prerelease/ios/recipes/xcode_help-IB_storyboard/Chapters/BackwardCompatibilityofActiveSegues.html 2. UIViewController,https://developer.apple.com/library/prerelease/tvos/documentation/UIKit/Reference/UIViewController_Class/
prettyprint
2015年9月15日 星期二
第 7 個程式:導覽控制器 Navigator 與 Segue
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言