開始用Swift開發(fā)iOS 10 - 10 Navigation Controller的介紹和Segue

接著上一篇開始用Swift開發(fā)iOS 10 - 9 Table Row的刪除, UITableViewRowAction和UIActivityViewController的使用的代碼,繼續(xù)學(xué)習(xí)Navigation Controller和Segue。

創(chuàng)造Navigation Controller

  • 選中Restaurant Table View Controller然后點(diǎn)擊菜單欄中的Editor > Embed in > Navigation Controller;生成了新的一Scene(Navigation Controller Scene)。
  • 選中之前Scene中的Navigation bar,修改其titleFood Pin。

添加 Detail View Controller

之前添加導(dǎo)航欄,現(xiàn)在添加另一個(gè)view controller,用來顯restaurant的詳細(xì)內(nèi)容。

  • 拖動(dòng)一個(gè)新的View Controller。
  • 拖動(dòng)一個(gè)image View,調(diào)整合適大小,并添加相關(guān)約束。Content Mode屬性修改為 Aspect Fill。
  • 用segue連接prototype cell和detail scene。 Ctrl-drag從prototype cell到detail scene。如果不方便選擇prototype cell,可以在document outline中拖動(dòng)。選擇Show連接風(fēng)格。
  • iOS 10中有幾種sugue類型:

    • Show:新的view controller將被添加view controller棧的頂部。跳轉(zhuǎn)的頁面有Navigation bar,并且有返回原來頁面的返回按鈕。這是非常常用的的類型。
    • Show detail:在view controller棧中,新的view controller將被替代原來的view controller。跳轉(zhuǎn)的頁面沒有Navigation bar,也沒有返回原來頁面的返回按鈕。
    • Present modally:新頁面將以動(dòng)畫形式從底部出現(xiàn)到覆蓋整個(gè)手機(jī)屏幕。這種形式最常見的列子是iOS自帶的日歷應(yīng)用:
    • Present as popover:以帶有箭頭錨點(diǎn)的彈框顯示。通常使用的iPad應(yīng)用中
  • 不再需要action sheet,去除tableView(_:didSelectRowAt:)方法

  • 創(chuàng)建新類文件RestaurantDetailViewController,繼承至UIViewController。是新建scene與RestaurantDetailViewController的聯(lián)系。

  • RestaurantDetailViewController中加入一個(gè)outlet和一個(gè)變量,outlet關(guān)聯(lián)Image View,restaurantImage用與接受table view傳來的圖像名稱數(shù)據(jù)。

    @IBOutlet var restaurantImageView:UIImageView!
    var restaurantImage = ""

    override func viewDidLoad() {
        super.viewDidLoad()

        restaurantImageView.image = UIImage(named: restaurantImage)
    }

通過segues傳遞數(shù)據(jù)

segue管理view controller之間的過渡。當(dāng)segue觸發(fā)時(shí),storyboard會通知源view controller(如RestaurantTableViewController)調(diào)用方法prepare(for:sender:),可以通過此方法傳遞數(shù)據(jù)。

  • 當(dāng)storyboard中結(jié)構(gòu)復(fù)雜時(shí),segue可能會有很多,同一個(gè)view controller可能與各級view controller之間有segue,這種情況最好給segue一個(gè)唯一identifier。設(shè)置segue的identifier為 showRestaurantDetail。

  • RestaurantTableViewController中添加prepareForSegue:

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "showRestaurantDetail" {
            if let indexPath = tableView.indexPathForSelectedRow {
                let destinationController = segue.destination as! RestaurantDetailViewController
                destinationController.restaurantImage = restaurantImages[indexPath.row]
            }
        }
    }

為詳情頁面添加三個(gè)標(biāo)簽

  • 添加三個(gè)標(biāo)簽,Text分別為NameLocation,Type(注意:雖然Text會被替代,但不要開始留空,因?yàn)榱艨諘怪笊蓅tack view時(shí),大小不確定);三個(gè)label生成一個(gè)stack view,調(diào)整字體和大小,添加適當(dāng)?shù)募s束。
  • 添加三個(gè)outlet,和三個(gè)與之對應(yīng)的變量。
    @IBOutlet var nameLabel: UILabel!
    @IBOutlet var locationLabel: UILabel!
    @IBOutlet var typeLabel: UILabel!
    
    var nameText = ""
    var locationText = ""
    var typeText = ""
    
    override func viewDidLoad() {
        super.viewDidLoad()

        restaurantImageView.image = UIImage(named: restaurantImage)
        nameLabel.text = nameText
        locationLabel.text = locationText
        typeLabel.text = typeText
    }
  • 修改RestaurantTableViewController中的prepareForSegue方法:
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "showRestaurantDetail" {
            if let indexPath = tableView.indexPathForSelectedRow {
                let destinationController = segue.destination as! RestaurantDetailViewController
                destinationController.restaurantImage = restaurantImages[indexPath.row]
                destinationController.nameText = restaurantNames[indexPath.row]
                destinationController.locationText = restaurantLocations[indexPath.row]
                destinationController.typeText = restaurantTypes[indexPath.row]
            }
        }
    }

代碼

Beginning-iOS-Programming-with-Swift

說明

此文是學(xué)習(xí)appcode網(wǎng)站出的一本書 《Beginning iOS 10 Programming with Swift》 的一篇記錄

系列文章目錄

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容