一种storyboard+swift实现页面跳转的方法
如題。視圖控制器A顯示視頻列表;視圖控制器B顯示視頻詳情,現希望將兩個視圖關聯起來,點擊A中某個視頻跳轉到B。
?
作為iOS小菜鳥我首先搜索了一下關鍵詞 “tableviewcell 跳轉”,然而搜索結果多為object-C、xib、.m文件相關的文檔,已看暈,最后在stackoverflow上找到一篇看得懂的問答。題主在Storyboard上將UITableViewCell與視圖控制器B關聯起來,但是并不知道如何關聯數據。
題目下方有網友表示:“實現prepareForSegue方法就好啦!”
那么prepareForSegue究竟屬于哪個protocol或class,如何在類中實現prepareForSegue方法,segue的生命周期又是怎樣的呢?經過查閱文檔得出如下結論:
1. 定義:Segue表示storyboard文件中兩個ViewController之間的轉換(?)。通常由A視圖控制器的按鈕、表格行或手勢指向B視圖控制器。
2. 觸發(fā):由UIKit實現,可使用notifications在A、B間傳輸數據。segue被觸發(fā)后工作流程如下,提供shouldPerformSegueWithIdentifier:sender:方法中止跳轉所需的步驟,如不新建segue和B;提供prepareForSegue:sender:方法傳輸數據。
?
3. 類型:show是把B堆在A上,detail用B替換A,Modally用模版顯示B,Popover用B作彈窗。
| Show (Push) | This segue displays the new content using the?showViewController:sender:?method of the target view controller. For most view controllers, this segue presents the new content modally over the source view controller. Some view controllers specifically override the method and use it to implement different behaviors. For example, a navigation controller pushes the new view controller onto its navigation stack.? UIKit uses the?targetViewControllerForAction:sender:method to locate the source view controller. |
| Show Detail (Replace) | This segue displays the new content using the?showDetailViewController:sender:?method of the target view controller. This segue is relevant only for view controllers embedded inside a?UISplitViewController?object. With this segue, a split view controller replaces its second child view controller (the detail controller) with the new content. Most other view controllers present the new content modally. UIKit uses the?targetViewControllerForAction:sender:method to locate the source view controller. |
| Present Modally | This segue displays the view controller modally using the specified presentation and transition styles. The view controller that defines the appropriate presentation context handles the actual presentation.? |
| Present as Popover | In a horizontally regular environment, the view controller appears in a popover. In a horizontally compact environment, the view controller is displayed using a full-screen modal presentation. |
?
那么接下來該行動啦!
1. 選中cell,關聯cell與B,segue類型選擇selection show (detail)
2. 在A對應的Controller中覆蓋prepareForSegue方法,把數據傳給B
class AController: UIViewController, UITableViewDataSource,UITableViewDelegate {
? ? // ...
? ??// 目前只有一個segue,所以沒有判斷viewControllerId,產生錯誤再學怎么區(qū)分
? ? override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
? ? ? ? let index : Int = (self.table.indexPathForSelectedRow?.row)!
? ? ? ? let data:VideoSummary = videoSummaries[index]
? ? ? ? let view : BController = segue.destinationViewController as! VideoViewController
? ? ? ? view.selectedVideo = data
? ? }
}
?
3. 在B對應的Controller中添加selectedVideo屬性
class BController: UIViewController , UITableViewDataSource,UITableViewDelegate {
? ? var selectedVideo : VideoSummary! // 注意感嘆號
? ? //...
}
4. 在B對應的Controller中設置視頻詳情
class BController: UIViewController , UITableViewDataSource,UITableViewDelegate {
? ? //...
? ??override func viewDidLoad() {
? ? ??if(selectedVideo.title.containsString("...")){
? ? ? //...
? ?}
? ? //...
}
// 成功。
?
posted on 2016-06-15 21:47 大俠去哪兒 閱讀(...) 評論(...) 編輯 收藏轉載于:https://www.cnblogs.com/yinkw/p/5589008.html
總結
以上是生活随笔為你收集整理的一种storyboard+swift实现页面跳转的方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 初识 HTML5(一)
- 下一篇: 获取当前屏幕显示的viewcontrol