SwiftUI For Dummies. Wei-Meng Lee

Чтение книги онлайн.

Читать онлайн книгу SwiftUI For Dummies - Wei-Meng Lee страница 11

SwiftUI For Dummies - Wei-Meng Lee

Скачать книгу

@State private var label = "label"

      

       var body: some View {

       VStack {

       Button(action: {

       self.label = "Button tapped"

       }) {

       Text("Submit")

       .padding(EdgeInsets(

       top: 10, leading: 10,

       bottom: 10, trailing: 10))

       .background(Color.yellow)

       .foregroundColor(Color.black)

       .border(Color.gray, width: 3)

       .font(Font.system(size: 26.0))

       .overlay(

       RoundedRectangle(cornerRadius: 10)

       .stroke(Color.gray,

       lineWidth: 5)

       )

       }

       Text(label)

       .padding()

       }

       }

       }

      Notice that all the views are now created declaratively using code — no more drag-and-drop in Storyboard. Layouts are now also specified declaratively using code (the VStack in this example stacks all the views vertically). Delegates are now replaced with closures. More important, views are now a function of state (and not a sequence of events) — the text displayed by the Text view is now bound to the state variable label. When the button is tapped, you change the value of the label state variable, which automatically updates the text displayed in the Text view. This programming paradigm is known as reactive programming.

      To start developing using SwiftUI, you need the following:

       Xcode version 11 or later

       A deployment target (Simulator or real device) of iOS 13 or later

       macOS Mojave (10.14) or later (Note that if you're running macOS Mojave, you won’t be able to use Live Preview and design canvas features; full features are available only in macOS Catalina (10.15) and later.)

      Alternatively, if you have a paid Apple developer account (you need this if you want to make your apps available on the App Store, but this is not a requirement for trying out the examples in this book), head over to https://developer.apple.com, sign in, and download Xcode directly.

      FIGURE 1-3: Installing Xcode from the Mac App Store.

For this book, our focus is on developing iOS applications for the iPhone. Developing iPad, watchOS, tvOS, and macOS applications using SwiftUI is beyond the scope of this book.

      Hello, SwiftUI

      After you’ve installed Xcode, you’ll probably be very eager to try out SwiftUI. So, let’s take a dive into SwiftUI and see how it works! Follow these steps:

      1 Launch Xcode.

      2 Click Create a new Xcode project (see Figure 1-4).

      3 Select Single View App and click Next (see Figure 1-5).

      4 In the Product Name field, enter HelloSwiftUI (see Figure 1-6).

      5 In the Organization Name field, enter your name.

      6 In the Organization Identifier field, enter a unique identifier, such as the reverse domain name of your company.

      7 From the User Interface drop-down list, select SwiftUI.

      8 Click Next and save the project to a location on your Mac.You should see the project created for you (see Figure 1-7). The ContentView.swift file contains the UI for your application's main screen.

      FIGURE 1-4: Launching Xcode.

Screenshot for choosing a template for a Single View App project type and click Next.

      FIGURE 1-5: Selecting the Single View App project type.

Screenshot of naming the project.

      FIGURE 1-6: Naming the project.

Viewing the project that you’ve created.

Скачать книгу