Sometimes you need to create UI elements with code. This video shows how to do just that.
- [Instructor] Now that we've gone through many UI elements, let's look at creating one of them in code. The object that we'll create now is a UI switch. To do that, let's go inside viewDidLoad and create a switch, so let switchView equal UISwitch and here we'll specify a frame, that's a CGRect. Just press return on that, open up the parentheses, choose the option that asks for X, Y, width, and height of CGFloat type.
Choose that and then specify the location and size of the switch. Let's go 10 for X, 100 for Y, this is from the top-left, so 100 points down from the top-left. For the width, 100, height, 100, and then we'll add it to the screen, view.addSubView and then pass in the switchView. Let's run the application and see what it looks like.
There's a switch and we can toggle it on and off. Let's say we wanted to respond to when the switch is toggled on and off. Let's return to Xcode, close the application, and then what we need to do is connect an action to the switchView. So, switchView.add and we want to choose the one with target and action. Target's going to be self, that's the object that contains the code that we want to run, and the action we actually haven't defined yet, so let's define that real quick.
Type func switchDidChange, add some curly braces, and in here we'll put an underscore there and we'll call this switchView and the data type is going to be a UISwitch. In here we can simply print whether the switch is on or off.
I'll type is on and a question mark, a backslash, and some parentheses, and inside of the parentheses, switchView.isOn. Now, what we need to do is connect this method that we created to the action of the switchView. Before we do that, we need to call out this method as an objective C method. We need to expose it to objective C. If you've watched the first course in this series, then you've seen how to do that with a button.
You have to use the at objective C call-out. That exposes the method to objective C and allows you to add it as an action inside of the add target method. To do that, type a pound sign or a hash and then selector, and the method is going to be switch.DidChange. Then, we can just specify the event that we want to respond to. This is the event that happens with the switchView.
To do that, type a dot and then we're looking for valueChanged. Choose that and that should be it. We should be able to see the output inside of the debug area. Let's get out of full screen mode here and run the application and we should see this print statement running each time we toggle the switch on or off. There we go, it says is on is true and then false.
So, it's working. Stop the app and return to Xcode and go back into full screen. If you want to create a switch or just about any other user interface object in Xcode, create the object in code to handle any kind of event. Use the add target method specifying the target, which is normally going to be self, the action which must be exposed to objective C, and then the event that you want to handle. In many cases, it's going to be valueChanged.
Then, simply write the code that you want in the accompanying method.
- Alert controllers and actions
- Sliders, progress bars, and switches
- Creating UI elements with code
- Displaying web content with Safari view controllers and WKWebView
- Creating collection view controllers
- Creating tabbed apps
- Publishing your app with App Store Connect