iT邦幫忙

2021 iThome 鐵人賽

DAY 30
0
Mobile Development

麻瓜學習 iOS 開發系列 第 30

Day 30 : DetailView

前言

今天要將 RecipeFeaturedView 中的圖片變成可點開的,
點開後會顯示相應的 DetailView。

實作

  • 把圖片變成一個 button:

    宣告靜態屬性:
@State var isDetailViewShowing = false

將其帶入 sheet modifier 作為參數,
點擊這個按鈕時會將變數 isDetailViewShowing 變為 true,
這樣會顯示放置在 sheet 閉包裡的 view。
傳入 RecipeDetailView 實例:

sheet(isPresented: $isDetailViewShowing ) {
	RecipeDetailView(recipe: model.recipes[index])
	}

  • 進入 RecipeFeaturedVIew 將 Preparation Time、Highlights 值修改為動態的
    宣告靜態屬性 tabSelectionIndex,並將其雙向綁定帶入 TabView:
@State var tabSelectionIndex = 0

要知道 RecipeFeaturedVIew 顯示的第一個食譜並不是 Recipe 中的第一個,
所以宣告 method:

func setFeaturedIndex() {
	var index = model.recipes.firstIndex { recipe in
            return recipe.featured
        }
        tabSelectionIndex = index ?? 0
    }

  • 動態傳入 Highlights 值
    因為想要在 RecipeFeaturedView 和 RecipeListView 中共同顯示動態的 Highlights,
    所以新建一個 Highlights file:

    在 RecipeFeaturedView 中將 hardcode text 改成:
RecipeHighlights(highlights: model.recipes[tabSelectionIndex].highlights)


preview:
在 RecipeListView 中添加:

preview:


上一篇
Day29: Picker controller
系列文
麻瓜學習 iOS 開發30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言