This course delves into SwiftUI, which you can use to create user interfaces for your apps. We're going to cover how to use it, how to create views, and how to leverage it to create responsive apps that work in all screen sizes.
This course is designed for anyone who wants to:
- Learn about iOS development and coding
- Move into a career as an iOS developer
- Master Swift skills
To get the most out of this course, you should have some basic knowledge of iOS.
Hi, within this lecture, we're going to see how to work with data in SwiftUI, and how to create buttons to create our own actions. So, let's go and open another view because we're going to work in a different view, and we have our own preview, so that we can do that whenever we want. So, let's go and create a new SwiftUI view, and I'm going to call this FourthView, and let's come over here and bring this FourthView to the top, and then hit 'Resume'. So, what I want to do in this lecture is to create a variable, and we're going to change that variable. For example, right now, we are displaying Hello World inside of that text. What if I want to create a variable and display that variable inside of this text, can I do that? Yep, of course we can. So, make sure you create your variable under your struct but above your body, and I'm going to call mine my name. So, myName will be "Atil" and rather than displaying Hello World, I'm going to display myName inside of this text. So, if I hit "Resume", I will see Atil here. So, this is good. Now, I want to add a button, and when I hit that button the name will change, can I do that? Of course, we can. Let's see how we can achieve that result. So, I'm going to wrap this around that VStack, because we're going to have more than one view. Okay, I'm going to have my text and I'm going to have a button. So, if you write 'Button' over here, you will see the button. And if you're putting parentheses, it will ask you for some parameters. And the parameters that we are looking for is the action and the label. So, the action is the actual action. If you hit 'Enter' over here, it will create a coding block for you in which you can write your own code, and whenever you tap on this button, this will happen. We're going to change our myName variable inside of this action. And for label, we generally just hit 'Enter' and write whatever we want inside of that label, for example, a text, or if we just want to show an image for a button, then we can just bring in the image. But this time if I hit 'Enter', it will create a weird coding block for me, a closure for me, and I don't need that closure. Rather, I'm going to delete everything regarding to that closure, and I believe I deleted the curly braces as well. So, after the action, you can just open your own curly brace and over here, you can just write 'Text' and just choose your own button text like MyButton. And as you can see as I type, it will appear on the right hand side and as you can see now, we see the button over here. And the good thing about this preview is that you can hit this live button and make your preview live, so that you can actually test the actions as well. So right now, I don't have any action but if I had, I can just click over that button using this preview rather than opening the simulator, and I can just test the actions like navigation, like button and everything inside of this preview. So, for example, I can hit my button right now, nothing will happen because I didn't write anything. So, let me give a .padding() to my text because they are very close to each other, and in fact, let me just give some large title to this font, so that we can see the name better, the name better. So, this is much better, and under my Button(action:, I want to change this name, right? So, how do we do that? It's fairly easy in Swift, you can just write myName is something else like James, for example. And this should change my name but we cannot do that. As you can see, it says that self is immutable and we get this problem when we work with structs. Remember, in structs we have the possibility of true immutability. Even we had to use some mutating functions in order to mutate our variables inside of these functions when we deal with structs before. And in this case, we have to change this variable, right? Because we are inside of this button and we cannot have some constant variables every time, and we have to change the variable at some point in our app. So, if you add @State in front of your variable, and of course, you have to add self. as well, then this will be okay. And this @State syntax is a property wrapper. When you add this @State it means that you get to change the variable. And also, when you change the variable it will re-render the whole view, so that you can actually see the change inside of your preview or inside of your app as well. For example, let me run this and hit my button, and as you can see, now it says James. So, whenever you make some variable @State variable, it means that when you change it, the view will get re-rendered, and we will see the change. Now, it works. So, do we have more like State? Yep, we have some more property wrappers as well. So, let me show you some tutorial on the web. I have seen this blog and I liked it, so I'm going to share this link with you. So, let me just go over here and find it, yep, this is the one, so I don't know these guys. So apparently, he's called Majid's but I believe he has actually written down this property wrappers in a good structure and you cannot even find that tutorial, you cannot even find these explanations in the Apple developer website, right now. So, I have read this property wrappers and I believe they are actually very well written. As you can see, there are a lot of property wrappers and we're going to see the most common ones in this section, and unfortunately, we won't have time to see everything related to SwiftUI. That's a subject for whole another course, and in fact, we have another course on SwiftUI with codestars. You can check this up if you want. But again, if you are capable of understanding everything about Swift, if you're very good at Swift and iOS development, you can always go to developer.apple.com and read the documentation and try to figure out on your own as well, because when you complete this course you will be more than comfortable to work with iOS, Development work with Swift, and create whatever project that you want. But keep in mind that we're going to learn the basics, the introduction part of the SwiftUI, and there is much more things that you have to learn in order to master this SwiftUI as well. So, let's stop here, and see one of the most important concepts called binding in the next lecture.
Atil is an instructor at Bogazici University, where he graduated back in 2010. He is also co-founder of Academy Club, which provides training, and Pera Games, which operates in the mobile gaming industry.