iT邦幫忙

2021 iThome 鐵人賽

DAY 23
0
Mobile Development

程式初學就來點swift吧!教你掌握整個蘋果生態圈!系列 第 23

Day 23 - SwiftUI開發實作2 (多愛女朋友測試APP、Alert用法、傳遞變數)

今天一樣是實作,不過今天實作就比較稍微不一樣,我們會先講alert的用法,並且講解如何在不同View裡傳遞變數。

Alert

有兩種形式,顯示或是不顯示。

所以我們一樣也要去設定變數

struct c3: View {
    @State private var showAlert = false
      var body: some View {
         Button(action: {
            showAlert = true
         }) {
            VStack {
               Text("酷嗎")
            }
         }.alert(isPresented: $showAlert) { () -> Alert in
            let answer = ("n")
            return Alert(title: Text(answer))
         }
       }
    }

按下Button就可以觸發Alert,Alert 會彈出對話框包含一個答案。

將ContentView的值傳給SecondView

在這邊有非常多的做法,我這邊使用 @State and @Binding。

@State var c1 = ""
@Binding var c1:String

寫法就是View1 用@State,View2 用@Binding 。

不過我們在NavigationLink中使用要使用以下做法:

NavigationLink(
                destination: t2(c1: $c1),
                label: {
                    Text("第二題")
                    
                })

正文

一樣,我們先開啟一個新的專案。

接著我們在ContentView設一個NavigationView。接著在裡面包一個我們NavigationLink,並且指向目的地為t1。

接下來我們開一個新的swiftui。我們把它命名為t1。

接下來我們在t1結構裡面寫裡面寫文字、文字框,輸入的數字我們設為c1。一樣我們底下在寫一個NavigationLink。

記得使用我們上面的做法傳值。

接著我們直接在底下見一個新的結構t2,按照一樣的做法建立第二個題目。用@Binding讀到t1的c1變數。

接著我們建立一個Button叫出Alert為得分題可。

我們可以直接在Alert中做運算,不需外加func。

以下為完整程式範例:

struct t1: View {
    @State var sum = 0
    @State var c1 = ""
    var body: some View {
        VStack{
        Text("Q1:願意每天打2個小時的電話 1~10分")
        TextField("請輸入", text: $c1)
                        .padding()
                        .overlay(
                        RoundedRectangle(cornerRadius: 10)
                            .stroke(Color.pink, lineWidth: 5)
                        )
            NavigationLink(
                destination: t2(c1: $c1),
                label: {
                    Text("第二題")
                    
                })
        }.navigationTitle("第一題")
    }
}

struct t2: View{
    @Binding var c1:String
    @State var c2 = ""
    @State private var showAlert = false
    var body:some View {
        VStack{
        Text("Q2:願意每週花一天陪女朋友出門逛街 1~10分")
        TextField("請輸入", text: $c2)
                        .padding()
                        .overlay(
                        RoundedRectangle(cornerRadius: 10)
                            .stroke(Color.pink, lineWidth: 5)
                        )
            Button(action: {showAlert = true}) {
                            HStack {
                                Image(systemName: "person.fill.checkmark")
                                    .font(.title3)
                                Text("點我看得分吧!")
                                    .fontWeight(.semibold)
                                    .font(.title)
                            }
            }.alert(isPresented: $showAlert) { () -> Alert in
                let answer = (Int(c1) ?? 0) + (Int(c2) ?? 0)
                return Alert(title: Text(String(answer)))
             }
    }.navigationTitle("第二題")
        
}
}

總結

我個人覺得今天學得非常實用,好像很少中文教程把不同View取得變數這件事寫的夠詳細,所以這邊把它寫出來給各位,並且今天加入了Alert給各位。


上一篇
Day 22 - SwiftUI開發實作1 (簡易計算機)
下一篇
Day 24 - Watch os 開發學習1
系列文
程式初學就來點swift吧!教你掌握整個蘋果生態圈!30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言